I want to create my first game.
Nothing special, just blue rectangle moving when WSAD keys are pressed.
The problem is that when I run my game, there are bugs with rectangle(See image below). Bugs appears only during horizontal movement, and not vertical.
Which is interesting, when I changed line:
renderer = SDL_CreateRenderer(display, -1, SDL_RENDERER_ACCELERATED)
to:
renderer = SDL_CreateRenderer(display, -1, SDL_RENDERER_SOFTWARE)
everything is OK
I am using Windows 10, MinGw with CMake(C++14), and SDL 2.0.8, Intel core i5 7th gen, Radeon M7 R465
Im my code OnRender
function is responsible for rendering, maybe I made something wrong in it?(Function in my code posted at end of question)
I am also using SDL_WINDOW_OPENGL
flag to create my window, but changing it to SDL_WINDOW_SHOWN
doesn't change anything.
#include <SDL2/SDL.h>
class Game
{
private:
SDL_Surface *display_surf = nullptr;
SDL_Renderer *renderer = nullptr;
SDL_Window *display = nullptr;
private:
bool running, prW = false, prS = false, prD = false, prA = false;
int x, y;
int spd_y, spd_x;
int scr_w, scr_h;
public:
Game();
int OnExecute();
public:
bool OnInit();
void OnEvent( SDL_Event *event );
void OnLoop();
void OnRender();
void OnCleanup();
};
Game::Game()
{
running = false;
}
int Game::OnExecute()
{
if( !OnInit() )
{
return -1;
}
running = true;
SDL_Event event;
while( running )
{
while( SDL_PollEvent( &event ) )
{
OnEvent( &event );
}
OnLoop();
OnRender();
SDL_Delay( 1 );
}
OnCleanup();
return 0;
}
bool Game::OnInit()
{
if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 )
{
return false;
}
SDL_DisplayMode dspm;
if( SDL_GetDesktopDisplayMode( 0, &dspm ) < 0 )
{
return false;
}
scr_h = dspm.h;
scr_w = dspm.w;
if( ( display = SDL_CreateWindow( "Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1920, 1080,
SDL_WINDOW_OPENGL ) ) == nullptr )
{
return false;
}
display_surf = SDL_GetWindowSurface( display );
if( ( renderer = SDL_CreateRenderer( display, -1, SDL_RENDERER_ACCELERATED ) ) == nullptr )
{
return false;
}
x = 0;
y = 0;
spd_x = 0;
spd_y = 0;
SDL_SetWindowFullscreen( display, SDL_WINDOW_FULLSCREEN );
return true;
}
void Game::OnEvent( SDL_Event *event )
{
if( event->type == SDL_QUIT )
{
running = false;
return;
}
switch( event->type )
{
case SDL_KEYDOWN:
switch( event->key.keysym.sym )
{
case SDLK_w:
if( prS )
{
spd_y = 0;
}
else
{
spd_y = -5;
}
prW = true;
break;
case SDLK_s:
if( prW )
{
spd_y = 0;
}
else
{
spd_y = 5;
}
prS = true;
break;
case SDLK_d:
if( prA )
{
spd_x = 0;
}
else
{
spd_x = 5;
}
prD = true;
break;
case SDLK_a:
if( prD )
{
spd_x = 0;
}
else
{
spd_x = -5;
}
prA = true;
break;
default:
return;
}
break;
case SDL_KEYUP:
switch( event->key.keysym.sym )
{
case SDLK_w:
if( !prS )
{
spd_y = 0;
}
else
{
spd_y = 5;
}
prW = false;
break;
case SDLK_s:
if( !prW )
{
spd_y = 0;
}
else
{
spd_y = -5;
}
prS = false;
break;
case SDLK_a:
if( !prD )
{
spd_x = 0;
}
else
{
spd_x = 5;
}
prA = false;
break;
case SDLK_d:
if( !prA )
{
spd_x = 0;
}
else
{
spd_x = -5;
}
prD = false;
break;
default:
return;
}
default:
return;
}
}
void Game::OnLoop()
{
x += spd_x;
y += spd_y;
if( x < 0 )
{
x = 0;
}
else if( x > scr_w - 100 )
{
x = scr_w - 100;
}
if( y < 0 )
{
y = 0;
}
else if( y > scr_h - 100 )
{
y = scr_h - 100;
}
}
void Game::OnRender()
{
SDL_SetRenderDrawColor( renderer, 0, 0, 0, 0x00 );
SDL_RenderClear( renderer );
SDL_Rect charc;
charc.x = x;
charc.y = y;
charc.w = 100;
charc.h = 100;
SDL_SetRenderDrawColor( renderer, 0, 0, 0xff, 0 );
SDL_RenderFillRect( renderer, &charc );
SDL_RenderPresent( renderer );
}
void Game::OnCleanup()
{
SDL_DestroyWindow( display );
SDL_Quit();
}
int main( int argc, char** argv )
{
Game game;
return game.OnExecute();
}