core update
authorksg <ken@mihrtec.com>
Fri, 29 Jul 2016 01:46:06 +0000 (18:46 -0700)
committerksg <ken@mihrtec.com>
Fri, 29 Jul 2016 01:46:06 +0000 (18:46 -0700)
src/Makefile
src/core/engine.h
src/core/io.c
src/core/main.c
src/core/state.c [new file with mode: 0644]
src/core/trigger.h

index c6262fd..894514a 100644 (file)
@@ -89,3 +89,8 @@ ifdef MISSINGLIBS
 include $(foreach lib,$(MISSINGLIBS),.make/lib$(lib).mk)
 endif
 
+#include rules for making each app
+ifdef APPTARGS
+include $(foreach app,$(APPTARGS),.make/app$(app).mk)
+endif
+
index 9af038f..42cadba 100644 (file)
@@ -3,14 +3,14 @@
   \details Contains various global compile-time defines such as rendering FPS
            which loads initial game data, before finally invoking the
            main loop gameloop(void)
-  \author  K
+  \author  Mihrtec
   \date    2016
    ------------------------------------------------------------------------------*/
 #ifndef _ENGINE_H_
 #define _ENGINE_H_
 
-#ifndef __EMSCRIPTEN__
-#define LOOP_YIELD_OPTIONAL
+#ifdef __EMSCRIPTEN__
+#define NON_BLOCKING_LOOPS
 #endif
 
 /* debug level */
@@ -33,4 +33,3 @@
 #define TARGET_DT (1000 / TARGET_FPS)
 
 #endif //_ENGINE_H_
-
index 631a452..dba85ae 100644 (file)
@@ -30,13 +30,17 @@ void        io_quit(void);
 /** IO initializer */
 int
 io_init()
-{}
+{ return 0;
+}
 
 const char*
 io_get_error()
-{}
+{ static char err[5] = "Ass!";
+  return (const char*)&err;
+}
 
 void
 io_quit()
-{}
+{
+}
 
index 6da2201..5e18ac5 100644 (file)
@@ -2,8 +2,9 @@
   \brief   engine entry
   \details initializes necessary subsystems before invoking the preloader,
            which loads initial game data, before finally invoking the
-           main loop gameloop(void)
-  \author  K
+           main loop main_loop(void), which may be run in either blocking
+           or non-blocking mode.
+  \author  Mihrtec
   \date    2016
 ------------------------------------------------------------------------------*/
 #ifdef __EMSCRIPTEN__
@@ -27,7 +28,6 @@
 #define TRIGGERS quit_trigger
 #include <core/trigger.h>
 #include <core/engine.h>
-#include <wolfssl/wolfcrypt/sha256.h>
 
 /* exposed functions */
 void main_loop(void);
@@ -35,15 +35,17 @@ void main_loop(void);
 /* private functions */
 static int main_init(void);
 
-/* unexposed externs *
+/* unexposed externs */
 extern int         state_init(void);
 extern void        state_tick(uint32_t delta_ticks);
 extern const char* state_get_error(void);
 extern void        state_quit(void);
-extern void        state_handle_event(SDL_Event event);
+extern void        state_handle_event(SDL_Event *event);
+#if 0
 extern int         io_init(void);
 extern const char* io_get_error(void);
-extern void        io_quit(void);*/
+extern void        io_quit(void);
+#endif //TODO
 
 /* main jump buffer */
 jmp_buf jmp_main;
@@ -58,20 +60,17 @@ jmp_buf jmp_main;
   with the exit codes in core.h
 */
 #ifdef __EMSCRIPTEN__
-#define main_loop()\
-  emscripten_set_main_loop(main_loop,0,0);\
-  TRIGGER_SET(quit_trigger, emscripten_cancel_main_loop);\
-  return 0;
+#define main_loop()                                         \
+  do {                                                      \
+    emscripten_set_main_loop(main_loop,0,0);                \
+    TRIGGER_SET(quit_trigger, emscripten_cancel_main_loop); \
+    return 0;                                               \
+  } while (0)
 #endif
 
 int
 main (int argc, char** argv)
-{ Sha256 shstr = {0};
-  wc_InitSha256(&shstr);
-
-  printf("%d %d\n", sizeof(long), sizeof(long long));
-
-  switch(setjmp(jmp_main))
+{ switch(setjmp(jmp_main))
     { case 0:
         if (main_init())
           return -1;
@@ -92,37 +91,39 @@ main (int argc, char** argv)
 #endif
 
 /** subsystem initializer
-    Calling main_init() bootstraps the system, and may be called multiple
+    Calling main_init() boots the system, and may be called multiple
     times to cause a system-wide reboot.
   @return 0 if successful, -1 SDL, -2 IMG, -3 TTF, -4 STATE.
   SDL and logging is available after this is called
  ******************************************************************************/
-#define INIT(_subsys_id,_cond,_errorstring,_quit)\
-  if (_cond)\
-    { fprintf(stderr, #_cond " failed: %s\n", _errorstring());\
-      return -_subsys_id;\
-    }\
-  TRIGGER_SET(quit_trigger, _quit)
+#define INIT(_cond,_errorstring,_quit)                           \
+  do {                                                           \
+    if (_cond)                                                   \
+      { fprintf(stderr, #_cond " failed: %s\n", _errorstring()); \
+        return -1;                                               \
+      }                                                          \
+    TRIGGER_SET(quit_trigger, _quit);                            \
+  } while (0)
 
 #define SDL_FLAGS SDL_INIT_EVERYTHING & ~(SDL_INIT_TIMER | SDL_INIT_HAPTIC)
 
-static
-int
+static int
 main_init()
-{ static char bInitialized = 0;
+{ static uint32_t bInitialized = 0;
   if (bInitialized++)
     { TRIGGER(quit_trigger);
       SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Resetting [%d]\n",
                   bInitialized);
     }
 
-  INIT(1, SDL_Init(SDL_FLAGS) < 0, SDL_GetError, SDL_Quit);
-  INIT(2, IMG_Init(IMG_INIT_PNG) != IMG_INIT_PNG, IMG_GetError, IMG_Quit);
-  INIT(3, TTF_Init() == -1, TTF_GetError, TTF_Quit);
+  INIT(SDL_Init(SDL_FLAGS) < 0, SDL_GetError, SDL_Quit);
+  INIT(IMG_Init(IMG_INIT_PNG) != IMG_INIT_PNG, IMG_GetError, IMG_Quit);
+  INIT(TTF_Init() == -1, TTF_GetError, TTF_Quit);
+  INIT(state_init(), state_get_error, state_quit);
 
 /*TODO:
-  INIT(4, io_init(), io_get_error, io_quit);
-  INIT(5, state_init(), state_get_error, state_quit);
+  INIT(io_init(), io_get_error, io_quit);
+  INIT(render_init(), io_get_error, io_quit);
 */
 
   SDL_Log("Initialization Complete.");
@@ -133,46 +134,37 @@ main_init()
  *******************************************************************************/
 void
 main_loop()
-{ static uint32_t main_loop_last_ticks = 0;
+{ static uint32_t state_last_ticks = 0;
   SDL_Event event;
   uint32_t delta_ticks;
 
-#ifdef LOOP_YIELD_OPTIONAL
+#ifndef NON_BLOCKING_LOOPS
 loop:
 #endif
-  /* Poll events (user/system inputs) */
+  /* user/system inputs */
   while (SDL_PollEvent(&event))
-    //state_handle_event(&event);
-    ;
+      state_handle_event(&event);
 
   /* change in time since last loop */
-  delta_ticks = SDL_GetTicks() - main_loop_last_ticks;
-
+  delta_ticks = SDL_GetTicks() - state_last_ticks;
   /* handle breakpoints (long pause likely a breakpoint) */
   if (delta_ticks > 1000)
     { SDL_LogWarn(SDL_LOG_CATEGORY_SYSTEM, "Recovering from long pause.");
       delta_ticks = TARGET_DT;
     }
-
-  /* tick the state manager forward by the change in time */
-  //state_tick(delta_ticks);
-  main_loop_last_ticks = SDL_GetTicks();
-
-  /* render the scene to backbuffer */
-////TODO: state_tick(delta_ticks);
-  /* swap in the backbuffer for display */
-////TODO: state_render();
-
-#ifdef LOOP_YIELD_OPTIONAL
-#define DIV_UP(x,y) ((x + (y / 2)) / y)
-
-  /* cap the framerate if we're handling the loop mechanism directly, but only
-     yield if we have a substantial portion of time to yield (1/10th of the
-     target delta) */
-  delta_ticks = SDL_GetTicks() - main_loop_last_ticks;
-  if ((delta_ticks + DIV_UP(TARGET_DT, 10)) < TARGET_DT)
+  /* tick the state manager and immediately save the time */
+  state_tick(delta_ticks);
+  state_last_ticks = SDL_GetTicks();
+
+  //TODO: render(screen)
+  //TODO: SDL_Flip(screen)::sdl_geterror
+
+#ifndef NON_BLOCKING_LOOPS
+#define DIV_UP(x,y) (((x) + ((y) / 2)) / (x))
+  /* yield if we have a substantial portion of time leftover */
+  delta_ticks += SDL_GetTicks() - state_last_ticks;
+  if (delta_ticks < (TARGET_DT - DIV_UP(TARGET_DT, 10)))
     SDL_Delay(TARGET_DT - delta_ticks);
-
   goto loop;
 #endif
 }
diff --git a/src/core/state.c b/src/core/state.c
new file mode 100644 (file)
index 0000000..40eb09e
--- /dev/null
@@ -0,0 +1,49 @@
+/*!@file
+  \brief   main state machine for the engine
+  \details initializes necessary subsystems before invoking the preloader,
+  which loads initial game data, before finally invoking the
+  main loop gameloop(void)
+  \author  Mihrtec
+  \date    2016
+  ------------------------------------------------------------------------------*/
+#ifdef __EMSCRIPTEN__
+#include <SDL_ttf.h>
+#include <emscripten/emscripten.h>
+#else
+#include <SDL2/SDL_ttf.h>
+#endif
+#include <SDL2/SDL.h>
+#include <SDL2/SDL_image.h>
+
+/* Exposed functions */
+int         state_init(void);
+void        state_tick(uint32_t);
+const char* state_get_error(void);
+void        state_quit(void);
+void        state_handle_event(SDL_Event *);
+
+int
+state_init()
+{ return 0;
+}
+
+void
+state_tick(uint32_t delta_ticks)
+{
+}
+
+const char*
+state_get_error()
+{ static char err[5] = "Ass!";
+  return (const char *)&err;
+}
+
+void
+state_quit()
+{
+}
+
+void
+state_handle_event(SDL_Event *event)
+{
+}
index 1f1878d..5c3c64d 100644 (file)
@@ -48,7 +48,7 @@
            }
 
 
-  \author  K
+  \author  Mihrtec
   \date    2016
   -----------------------------------------------------------------------------*/
 
 #endif
 
 /* Internal macro prototypes */
-#define _TRIGGER_DEL(TARG) --TARG.num_funcs
-#define _TRIGGER_POP(TARG) (TARG.func[_TRIGGER_DEL(TARG)])()
-#define _TRIGGER_SET(TARG, FUNC) TARG.func[TARG.num_funcs++] = FUNC
-#define _TRIGGER(TARG) while(TARG.num_funcs) TRIGGER_POP(TARG)
+#define _TRIGGER_DEL(TARG) --((TARG).num_funcs)
+#define _TRIGGER_POP(TARG) ((TARG).func[_TRIGGER_DEL(TARG)])()
+#define _TRIGGER_SET(TARG, FUNC) (TARG).func[(TARG).num_funcs++] = (FUNC)
+#define _TRIGGER(TARG) while((TARG).num_funcs) TRIGGER_POP(TARG)
 
 /* If TRIGGERS is not defined, use only a default trigger and define shorthand
    functions for easy use. */