Browse files

moved main gameloop from main

  • Loading branch information...
1 parent f793a31 commit a21f00a0eaae151df4222bf11b48976e0903008e @niksaak committed Feb 2, 2013
Showing with 109 additions and 76 deletions.
  1. +76 −0 src/engine/engine.c
  2. +10 −0 src/engine/engine.h
  3. +4 −71 src/main.c
  4. +0 −3 src/main.h
  5. +19 −2 src/state_main.c
View
76 src/engine/engine.c
@@ -0,0 +1,76 @@
+#include <stdbool.h>
+
+#include <SDL/SDL.h>
+#include <SDL/SDL_gfxPrimitives.h>
+#include <chipmunk/chipmunk.h>
+
+#include "engine.h"
+
+static bool running = true;
+static Uint32 phycount = 0;
+static Uint32 gfxcount = 0;
+static Uint32 timcount = 0;
+
+static void render(SDL_Surface* screen, void (*renderf)())
+{
+ void benchprint()
+ {
+ static char phstr[64] = {0};
+ static char gxstr[64] = {0};
+
+ if(timcount > 1000) {
+ snprintf(phstr, 64, "PHY FPS: %u", phycount);
+ snprintf(gxstr, 64, "GFX FPS: %u", gfxcount);
+ phycount = 0;
+ gfxcount = 0;
+ timcount = 0;
+ }
+
+ stringColor(screen, 10, screen->h - 20, phstr, 0xffffffff);
+ stringColor(screen, 10, screen->h - 10, gxstr, 0xffffffff);
+ }
+
+ SDL_FillRect(screen, NULL, 0);
+ (*renderf)();
+ benchprint();
+ SDL_UpdateRect(screen, 0, 0, 0, 0);
+}
+
+void gameloop(SDL_Surface* screen)
+{
+ Uint32 dt = 10;
+ Uint32 curt = SDL_GetTicks();
+ Uint32 accum = 0;
+
+ while(running) {
+ Uint32 newt;
+ Uint32 framet;
+ state_t* st;
+
+ ++gfxcount;
+ st = curstate();
+ newt = SDL_GetTicks();
+ framet = newt - curt;
+ curt = newt;
+ accum += framet;
+
+ while(accum >= dt) {
+ ++phycount;
+ st->do_world(dt / 1000.0);
+ accum -= dt;
+ }
+
+ render(screen, st->do_render);
+
+ if(st != curstate()) {
+ st->sleep();
+ curstate()->wake();
+ }
+ timcount += framet;
+ }
+}
+
+void endgame()
+{
+ running = false;
+}
View
10 src/engine/engine.h
@@ -0,0 +1,10 @@
+#include <SDL/SDL.h>
+#include <chipmunk/chipmunk.h>
+
+#include "stateman.h"
+#include "util.h"
+#include "system.h"
+
+extern void gameloop(SDL_Surface* screen);
+
+extern void endgame();
View
75 src/main.c
@@ -1,88 +1,21 @@
-#include <stdio.h>
-#include <stdbool.h>
-#include <SDL/SDL.h>
-#include <SDL/SDL_gfxPrimitives.h>
-
-#include "engine/system.h"
-#include "engine/util.h"
-#include "engine/stateman.h"
+#include "engine/engine.h"
#include "states.h"
#include "main.h"
-bool running = true;
-Uint32 run_counter = 9001;
-
-int timcount = 0;
-int phycount = 0;
-int gfxcount = 0;
-
-void benchprint()
-{
- SDL_Surface* win = NULL;
- static char phstr[64] = {0};
- static char gxstr[64] = {0};
-
- if((win = scrget()) == NULL)
- CRASH(SDL_GetError());
- if(timcount > 1000) {
- snprintf(phstr, 64, "PHY FPS: %i", phycount);
- snprintf(gxstr, 64, "GFX FPS: %i", gfxcount);
- timcount = 0;
- phycount = 0;
- gfxcount = 0;
- }
- stringColor(win, 10, win->h - 20, phstr, 0xffffffff);
- stringColor(win, 10, win->h - 10, gxstr, 0xffffffff);
-}
-
int main(int argc, char** argv)
{
state_t state; // main state IGNOT
- Uint32 dt = 10; // gameloop vars
- Uint32 curtime = 0;
- Sint32 accum = 0;
state = state_main(); // TODO: move this to init_states() or something
- scrstart("TEST", 512, 512, false); // window appearifies here
state.init(); // init our main-and-only (MANLY) gamestate
swstate(&state);
- curtime = SDL_GetTicks(); // initial timerset
- while(run_counter) {
- Uint32 newtime; // local gameloop vars
- Uint32 frametime;
- state_t* st;
-
- st = curstate();
- newtime = SDL_GetTicks(); // getting time for frame
- frametime = newtime - curtime;
- curtime = newtime;
-
- accum += frametime;
-
- while(accum >= dt) { // physics loop
- st->do_world(dt / 1000.0);
- accum -= dt;
- ++phycount;
- }
-
- SDL_FillRect(scrget(), NULL, 0); // rendering
- st->do_render();
- benchprint();
- SDL_UpdateRect(scrget(), 0, 0, 0, 0);
+ scrstart("TEST", 512, 512, false); // window appearifies here
+ gameloop(scrget());
+ scrstop();
- if(st != curstate()) { // put state to sleep if it in not current anymore
- st->sleep();
- curstate()->wake();
- }
- ++gfxcount;
- --run_counter;
- timcount += frametime;
- }
state.deinit();
- scrstop();
-
return 0;
}
View
3 src/main.h
@@ -1,4 +1 @@
#pragma once
-#include <stdbool.h>
-
-extern bool running;
View
21 src/state_main.c
@@ -2,8 +2,7 @@
#include <SDL/SDL.h>
#include <SDL/SDL_gfxPrimitives.h>
-#include "engine/system.h"
-#include "engine/util.h"
+#include "engine/engine.h"
#include "states.h"
struct {
@@ -46,6 +45,24 @@ static void wake()
static void do_world(cpFloat step)
{
+ void process_events()
+ {
+ SDL_Event e;
+
+ if(SDL_PollEvent(&e)) {
+ switch(e.type)
+ {
+ case SDL_QUIT:
+ endgame();
+ break;
+ default:
+ break;
+ }
+ }
+ }
+
+ process_events();
+
cpSpaceStep(space, step);
}

0 comments on commit a21f00a

Please sign in to comment.