Create basic engine
This commit is contained in:
parent
c02068f1ba
commit
b14d2f292a
@ -1,5 +1,3 @@
|
||||
add_library(engine
|
||||
foo.c
|
||||
)
|
||||
add_library(engine engine.c)
|
||||
set_default_target_options(engine)
|
||||
target_include_directories(engine PUBLIC include)
|
||||
|
66
engine/engine.c
Normal file
66
engine/engine.c
Normal file
@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Copyright (c) Camden Dixie O'Brien
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
#include "engine_hooks.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdatomic.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define FRAMERATE 60
|
||||
#define INTERVAL (1000 / FRAMERATE)
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int err = SDL_Init(SDL_INIT_VIDEO);
|
||||
assert(0 == err);
|
||||
|
||||
SDL_Window *window = SDL_CreateWindow(
|
||||
game_conf.win.title, SDL_WINDOWPOS_UNDEFINED,
|
||||
SDL_WINDOWPOS_UNDEFINED, game_conf.win.w, game_conf.win.h, 0);
|
||||
assert(NULL != window);
|
||||
|
||||
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);
|
||||
assert(NULL != renderer);
|
||||
|
||||
void *gamemem = calloc(1, game_conf.memsize);
|
||||
game_init(argc, argv, gamemem, renderer);
|
||||
|
||||
uint64_t prevt = SDL_GetTicks64();
|
||||
while (1) {
|
||||
uint64_t t = SDL_GetTicks64();
|
||||
uint64_t dt = t - prevt;
|
||||
|
||||
if (dt >= INTERVAL) {
|
||||
// Handle all events currently in queue
|
||||
SDL_Event evt;
|
||||
while (SDL_PollEvent(&evt)) {
|
||||
if (game_evthandle(gamemem, &evt) != GAMESTATUS_OK)
|
||||
goto quit;
|
||||
}
|
||||
|
||||
// Update game state
|
||||
if (game_update(gamemem, dt) != GAMESTATUS_OK)
|
||||
goto quit;
|
||||
|
||||
// Render frame
|
||||
SDL_RenderClear(renderer);
|
||||
game_render(gamemem, renderer, t);
|
||||
SDL_RenderPresent(renderer);
|
||||
|
||||
// Set previous frame draw time
|
||||
prevt = t;
|
||||
}
|
||||
|
||||
SDL_Delay(2);
|
||||
}
|
||||
|
||||
quit:
|
||||
game_teardown(gamemem);
|
||||
SDL_DestroyRenderer(renderer);
|
||||
SDL_DestroyWindow(window);
|
||||
SDL_Quit();
|
||||
}
|
11
engine/foo.c
11
engine/foo.c
@ -1,11 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) Camden Dixie O'Brien
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
#include "foo.h"
|
||||
|
||||
int foo(void)
|
||||
{
|
||||
return 42;
|
||||
}
|
34
engine/include/engine_hooks.h
Normal file
34
engine/include/engine_hooks.h
Normal file
@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright (c) Camden Dixie O'Brien
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
#ifndef ENGINE_HOOKS_H
|
||||
#define ENGINE_HOOKS_H
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
|
||||
typedef struct {
|
||||
struct {
|
||||
const char *title;
|
||||
unsigned w, h;
|
||||
} win;
|
||||
size_t memsize;
|
||||
} engineconf_t;
|
||||
|
||||
typedef enum {
|
||||
GAMESTATUS_OK,
|
||||
GAMESTATUS_QUIT,
|
||||
} gamestatus_t;
|
||||
|
||||
extern const engineconf_t game_conf;
|
||||
|
||||
void game_init(int argc, char *argv[], void *mem, SDL_Renderer *renderer);
|
||||
void game_teardown(void *mem);
|
||||
|
||||
gamestatus_t game_evthandle(void *mem, const SDL_Event *evt);
|
||||
gamestatus_t game_update(void *mem, unsigned dt);
|
||||
|
||||
void game_render(const void *mem, SDL_Renderer *renderer, long unsigned t);
|
||||
|
||||
#endif
|
@ -1,11 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) Camden Dixie O'Brien
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
#ifndef FOO_H
|
||||
#define FOO_H
|
||||
|
||||
int foo(void);
|
||||
|
||||
#endif
|
Loading…
x
Reference in New Issue
Block a user