Started on the engine proper
This commit is contained in:
2
core/common.c
Normal file
2
core/common.c
Normal file
@@ -0,0 +1,2 @@
|
||||
#include "common.h"
|
||||
|
||||
13
core/common.h
Normal file
13
core/common.h
Normal file
@@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
//platform exports/imports
|
||||
#if defined(__linux__)
|
||||
#define CORE_API extern
|
||||
#else
|
||||
#define CORE_API
|
||||
#endif
|
||||
|
||||
113
core/engine.c
Normal file
113
core/engine.c
Normal file
@@ -0,0 +1,113 @@
|
||||
#include "engine.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
//errors here should be fatal
|
||||
static void error(Engine* engine, char* message) {
|
||||
fprintf(stderr, message);
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
//exposed functions
|
||||
void initEngine(Engine* engine) {
|
||||
//clear
|
||||
engine->root = NULL;
|
||||
engine->running = true;
|
||||
|
||||
//init SDL
|
||||
if (SDL_Init(0) != 0) {
|
||||
error(engine, "Failed to initialize SDL2");
|
||||
}
|
||||
|
||||
//init the window
|
||||
engine->window = SDL_CreateWindow(
|
||||
"Caption",
|
||||
SDL_WINDOWPOS_UNDEFINED,
|
||||
SDL_WINDOWPOS_UNDEFINED,
|
||||
engine->screenWidth,
|
||||
engine->screenHeight,
|
||||
SDL_WINDOW_RESIZABLE
|
||||
);
|
||||
|
||||
if (engine->window == NULL) {
|
||||
error(engine, "Failed to initialize the window");
|
||||
}
|
||||
|
||||
//init the renderer
|
||||
engine->renderer = SDL_CreateRenderer(engine->window, -1, 0);
|
||||
|
||||
if (engine->renderer == NULL) {
|
||||
error(engine, "Failed to initialize the renderer");
|
||||
}
|
||||
|
||||
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "best");
|
||||
SDL_RenderSetLogicalSize(engine->renderer, engine->screenWidth, engine->screenHeight);
|
||||
}
|
||||
|
||||
void freeEngine(Engine* engine) {
|
||||
SDL_DestroyRenderer(engine->renderer);
|
||||
SDL_DestroyWindow(engine->window);
|
||||
SDL_Quit();
|
||||
|
||||
engine->renderer = NULL;
|
||||
engine->window = NULL;
|
||||
}
|
||||
|
||||
static void execStep(Engine* engine) {
|
||||
//DEBUG: for now, just poll events
|
||||
SDL_Event event;
|
||||
while (SDL_PollEvent(&event)) {
|
||||
switch(event.type) {
|
||||
//quit
|
||||
case SDL_QUIT: {
|
||||
engine->running = false;
|
||||
}
|
||||
break;
|
||||
|
||||
//window events are handled internally
|
||||
case SDL_WINDOWEVENT: {
|
||||
switch(event.window.event) {
|
||||
case SDL_WINDOWEVENT_RESIZED:
|
||||
SDL_RenderSetLogicalSize(engine->renderer, event.window.data1, event.window.data2);
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
//TODO: input
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//the heart of the engine
|
||||
void execEngine(Engine* engine) {
|
||||
//set up time
|
||||
gettimeofday(&engine->realTime, NULL);
|
||||
engine->simTime = engine->realTime;
|
||||
struct timeval delta = { .tv_sec = 0, .tv_usec = 1000 * 1000 / 60 }; //60 frames per second
|
||||
|
||||
while (engine->running) {
|
||||
//calc the time passed
|
||||
gettimeofday(&engine->realTime, NULL);
|
||||
|
||||
//if not enough time has passed
|
||||
if (engine->simTime.tv_sec < engine->realTime.tv_sec && engine->simTime.tv_usec < engine->realTime.tv_usec) {
|
||||
//while not enough time has passed
|
||||
while(engine->simTime.tv_sec < engine->realTime.tv_sec && engine->simTime.tv_usec < engine->realTime.tv_usec) {
|
||||
//simulate the world
|
||||
execStep(engine);
|
||||
|
||||
//calc the time simulation
|
||||
timeradd(&delta, &engine->simTime, &engine->simTime);
|
||||
}
|
||||
}
|
||||
else {
|
||||
SDL_Delay(10); //let the machine sleep, 10ms
|
||||
}
|
||||
|
||||
//render the world
|
||||
SDL_SetRenderDrawColor(engine->renderer, 0, 0, 0, 255); //NOTE: This line can be disabled later
|
||||
SDL_RenderClear(engine->renderer); //NOTE: This line can be disabled later
|
||||
SDL_RenderPresent(engine->renderer);
|
||||
}
|
||||
}
|
||||
30
core/engine.h
Normal file
30
core/engine.h
Normal file
@@ -0,0 +1,30 @@
|
||||
#pragma once
|
||||
|
||||
#include "common.h"
|
||||
#include "engine_node.h"
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
|
||||
#include <sys/time.h>
|
||||
|
||||
//the base engine object, which represents the state of the game
|
||||
typedef struct _engine {
|
||||
//engine stuff
|
||||
EngineNode* root;
|
||||
struct timeval simTime;
|
||||
struct timeval realTime;
|
||||
bool running;
|
||||
|
||||
//SDL stuff
|
||||
SDL_Window* window;
|
||||
SDL_Renderer* renderer;
|
||||
int screenWidth;
|
||||
int screenHeight;
|
||||
} Engine;
|
||||
|
||||
//APIs for initializing the engine
|
||||
CORE_API void initEngine(Engine* engine);
|
||||
CORE_API void freeEngine(Engine* engine);
|
||||
|
||||
CORE_API void execEngine(Engine* engine);
|
||||
|
||||
2
core/engine_node.c
Normal file
2
core/engine_node.c
Normal file
@@ -0,0 +1,2 @@
|
||||
#include "engine_node.h"
|
||||
|
||||
23
core/engine_node.h
Normal file
23
core/engine_node.h
Normal file
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include "common.h"
|
||||
|
||||
//forward declare
|
||||
typedef struct _engineNode EngineNode;
|
||||
typedef struct _engine Engine;
|
||||
|
||||
//the interface function
|
||||
typedef void (*EngineNodeFn)(EngineNode* self, Engine* engine);
|
||||
|
||||
//the node object, which forms a tree
|
||||
typedef struct _engineNode {
|
||||
//use Toy's memory model
|
||||
void* children;
|
||||
int capacity;
|
||||
int count;
|
||||
|
||||
EngineNodeFn onInit;
|
||||
EngineNodeFn onStep;
|
||||
EngineNodeFn onFree;
|
||||
} EngineNode;
|
||||
|
||||
43
core/makefile
Normal file
43
core/makefile
Normal file
@@ -0,0 +1,43 @@
|
||||
CC=gcc
|
||||
|
||||
IDIR+=.
|
||||
CFLAGS+=$(addprefix -I,$(IDIR)) -DSDL_MAIN_HANDLED -g -Wall -W -pedantic -Wno-unused-parameter -Wno-unused-function -Wno-unused-variable
|
||||
LIBS+=-lSDL2
|
||||
|
||||
ODIR = obj
|
||||
SRC = $(wildcard *.c)
|
||||
OBJ = $(addprefix $(ODIR)/,$(SRC:.c=.o))
|
||||
|
||||
OUTNAME=core
|
||||
|
||||
ifeq ($(findstring CYGWIN, $(shell uname)),CYGWIN)
|
||||
LIBLINE =-Wl,--out-implib=$(CORE_OUTDIR)/lib$(OUTNAME).dll.a -Wl,--export-all-symbols -Wl,--enable-auto-import -Wl,--whole-archive $(OBJ) -Wl,--no-whole-archive
|
||||
OUT=$(CORE_OUTDIR)/$(OUTNAME).dll
|
||||
else ifeq ($(shell uname),Linux)
|
||||
# No linux for the time being
|
||||
else ifeq ($(OS),Windows_NT)
|
||||
LIBLINE =-Wl,--out-implib=$(CORE_OUTDIR)/lib$(OUTNAME).dll.a -Wl,--export-all-symbols -Wl,--enable-auto-import -Wl,--whole-archive $(OBJ) -Wl,--no-whole-archive
|
||||
OUT=$(CORE_OUTDIR)/$(OUTNAME).dll
|
||||
else
|
||||
@echo "Platform test failed - what platform is this?"
|
||||
exit 1
|
||||
endif
|
||||
|
||||
library: $(OBJ)
|
||||
$(CC) -DCORE_EXPORT $(CFLAGS) -shared -o $(OUT) $(LIBLINE) $(LIBS)
|
||||
|
||||
static: $(OBJ)
|
||||
ar crs $(CORE_OUTDIR)/lib$(OUTNAME).a $(OBJ) $(LIBS)
|
||||
|
||||
$(OBJ): | $(ODIR)
|
||||
|
||||
$(ODIR):
|
||||
mkdir $(ODIR)
|
||||
|
||||
$(ODIR)/%.o: %.c
|
||||
$(CC) -c -o $@ $< $(CFLAGS)
|
||||
|
||||
.PHONY: clean
|
||||
|
||||
clean:
|
||||
$(RM) $(ODIR)
|
||||
Reference in New Issue
Block a user