Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions include/go_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,18 @@ namespace GoSDL {
// Called when closing the game
void closeAllGameControllers();

// Process game logic, drawing and rendering
void gameLoop();

/// Running flag
bool mShouldRun;

/// Active application flag
bool mApplicationActive;

/// Time interval between frames, in milliseconds
Uint32 mUpdateInterval;

/// Ticks recorded in last frame
Uint32 mLastTicks;

// Whether the mouse is in use
bool mMouseActive = false;

Expand Down
133 changes: 71 additions & 62 deletions src/go_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ using namespace std;
GoSDL::Window::Window (unsigned width, unsigned height, std::string caption, Uint32 updateInterval) :
mUpdateInterval(updateInterval)
{
// Get starting ticks
mLastTicks = SDL_GetTicks();

// Init SDL
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_GAMECONTROLLER) < 0)
{
Expand All @@ -39,7 +36,7 @@ GoSDL::Window::Window (unsigned width, unsigned height, std::string caption, Uin
#else
width, height,
#endif
SDL_WINDOW_RESIZABLE );
SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI );

// If window could not be created, throw an error
if (mWindow == NULL )
Expand All @@ -48,7 +45,7 @@ GoSDL::Window::Window (unsigned width, unsigned height, std::string caption, Uin
}

// Create renderer for the window
mRenderer = SDL_CreateRenderer( mWindow, -1, SDL_RENDERER_ACCELERATED );
mRenderer = SDL_CreateRenderer( mWindow, -1, 0 );

// If rendered could not be created, throw an error
if (mRenderer == NULL )
Expand Down Expand Up @@ -95,35 +92,30 @@ GoSDL::Window::~Window()

void GoSDL::Window::show()
{
// To store the ticks passed between frames
Uint32 newTicks;

// To poll events
SDL_Event e;

// Show the window
SDL_ShowWindow(mWindow);

mShouldRun = true;
mApplicationActive = true;

// Main game loop
while (mShouldRun) {

// Get ticks
newTicks = SDL_GetTicks();

// Get ticks from last frame and compare with framerate
if (newTicks - mLastTicks < mUpdateInterval)
{
SDL_Delay(mUpdateInterval - (newTicks - mLastTicks));
continue;
}

// Event loop
while (SDL_PollEvent (&e))
{
switch (e.type)
{
case SDL_APP_WILLENTERBACKGROUND:
mApplicationActive = false;
break;

case SDL_APP_WILLENTERFOREGROUND:
mApplicationActive = true;
break;

case SDL_QUIT:
// Yes, goto: http://stackoverflow.com/a/1257776/276451
Expand Down Expand Up @@ -169,60 +161,77 @@ void GoSDL::Window::show()
}
}

// Process logic
update();

// Process drawing
draw();

// Render the background clear
SDL_RenderClear (mRenderer);
if (mApplicationActive)
{
Uint32 gameCycleTicks = SDL_GetTicks();

// Iterator for drawing operations
GoSDL::DrawingQueueIterator qIt;
const GoSDL::DrawingQueueOperation * op;
gameLoop();

// Iterate all pending drawing operations
for (qIt = mDrawingQueue.begin(); qIt != mDrawingQueue.end(); ++qIt)
{
// Get a reference of the current operation
op = &(qIt->second);

// Set transparency
SDL_SetTextureAlphaMod(op->mTexture, op->mAlpha);

// Set coloring
SDL_SetTextureColorMod(op->mTexture, op->mColor.r, op->mColor.g, op->mColor.b);

// Draw the texture
int res = SDL_RenderCopyEx(this->mRenderer,
op->mTexture,
NULL,
&(op->mDstRect),
op->mAngle,
NULL,
SDL_FLIP_NONE);

// Check for errors when drawing
if (res != 0)
// Limit to framerate
gameCycleTicks = SDL_GetTicks() - gameCycleTicks;
if (gameCycleTicks < mUpdateInterval)
{
printf("ERROR %s \n", SDL_GetError());
SDL_Delay(mUpdateInterval - gameCycleTicks);
}
}
else
{
SDL_WaitEvent(nullptr);
}
}
// Exit point for goto within switch
exit:
;
}

void GoSDL::Window::gameLoop()
{
// Process logic
update();

// Process drawing
draw();

// Empty the drawing queue
mDrawingQueue.clear();
// Render the background clear
SDL_RenderClear (mRenderer);

// Update the screen
SDL_RenderPresent (mRenderer);
// Iterator for drawing operations
GoSDL::DrawingQueueIterator qIt;
const GoSDL::DrawingQueueOperation * op;

// Update the ticks
mLastTicks = newTicks;
// Iterate all pending drawing operations
for (qIt = mDrawingQueue.begin(); qIt != mDrawingQueue.end(); ++qIt)
{
// Get a reference of the current operation
op = &(qIt->second);

// Set transparency
SDL_SetTextureAlphaMod(op->mTexture, op->mAlpha);

// Set coloring
SDL_SetTextureColorMod(op->mTexture, op->mColor.r, op->mColor.g, op->mColor.b);

// Draw the texture
int res = SDL_RenderCopyEx(this->mRenderer,
op->mTexture,
NULL,
&(op->mDstRect),
op->mAngle,
NULL,
SDL_FLIP_NONE);

// Check for errors when drawing
if (res != 0)
{
printf("ERROR %s \n", SDL_GetError());
}
}

// Exit point for goto within switch
exit:
;
// Empty the drawing queue
mDrawingQueue.clear();

// Update the screen
SDL_RenderPresent (mRenderer);
}

void GoSDL::Window::close()
Expand Down