Blame src/world.c

3f9996
a20939
#include <time.h></time.h>
a20939
f63775
#include <sdl.h></sdl.h>
3f9996
3f9996
#include "private.h"
3f9996
3f9996
#include "world.h"
3f9996
3f9996
a20939
static int sdlInitialized = 0;
a20939
SDL_Window *window;
a20939
SDL_GLContext context;
1015c5
3f9996
static int started;
3f9996
static int stopped;
3f9996
static unsigned long long frameCount;
a20939
static unsigned long long elapsedTimeUs;
a20939
static unsigned long long elapsedTimeSinceLastFrameUs;
a20939
static unsigned int prevFrameTimeMs;
3f9996
3f9996
static Callback initCallback;
3f9996
static Callback drawCallback;
1015c5
static Callback deinitCallback;
3f9996
ba9f06
static int width = 512;
ba9f06
static int height = 512;
8eb855
static int resizable;
8eb855
static char title[1000];
8eb855
static int titleSize = (int)(sizeof(title)/sizeof(*title));
546e3a
static double frameRate = 24;
3f9996
3f9996
static int cameraEnabled;
3f9996
static double cameraX;
3f9996
static double cameraY;
3f9996
static double cameraZoom = 1;
3f9996
1015c5
static HeliArray keyEvents[6];
1015c5
static int keyEventsCount = (int)(sizeof(keyEvents)/sizeof(*keyEvents));
3f9996
3f9996
static int mouseMovedInFrame;
3f9996
static double _mouseX;
3f9996
static double _mouseY;
3f9996
3f9996
3f9996
1015c5
int keyEventGetCount(KeyEvent mode)
1015c5
	{ return (int)mode >= 0 && (int)mode <= keyEventsCount ? keyEvents[mode].count : 0; }
1015c5
const char *keyEventGet(KeyEvent mode, int i)
1015c5
	{ return (int)mode >= 0 && (int)mode <= keyEventsCount ? heliArrayGetValue(&keyEvents[mode], i) : NULL; }
1015c5
1015c5
static int keyEventCheck(KeyEvent mode, const char *code) {
1015c5
	int count = keyEventGetCount(mode);
1015c5
	for(int i = 0; i < count; ++i)
1015c5
		if (strcmp(keyEventGet(mode, i), code) == 0)
1015c5
			return TRUE;
1015c5
	return FALSE;
1015c5
}
1015c5
1015c5
static void keyEventAdd(KeyEvent mode, const char *code) {
1015c5
	if ((int)mode >= 0 && mode <= (int)keyEventsCount && !keyEventCheck(mode, code))
1015c5
		heliArrayInsert(&keyEvents[mode], -1, heliStringCopy(code), &free);
1015c5
}
1015c5
1015c5
static void keyEventRemove(KeyEvent mode, const char *code) {
1015c5
	if ((int)mode >= 0 && mode <= (int)keyEventsCount)
1015c5
		for(int i = keyEvents[mode].count-1; i >= 0; --i)
1015c5
			if (strcmp(keyEvents[mode].items[i].value, code) == 0)
1015c5
				heliArrayRemove(&keyEvents[mode], i);
1015c5
}
3f9996
1015c5
	
8535a3
int keyDown(const char *code)
1015c5
	{ return keyEventCheck(KEYEVENT_KEY_DOWN, code); }
07b70f
int keyWentDown(const char *code)
1015c5
	{ return keyEventCheck(KEYEVENT_KEY_WENTDOWN, code); }
07b70f
int keyWentUp(const char *code)
1015c5
	{ return keyEventCheck(KEYEVENT_KEY_WENTUP, code); }
3f9996
3f9996
int mouseDidMove()
3f9996
	{ return mouseMovedInFrame; }
8535a3
int mouseDown(const char *code)
1015c5
	{ return keyEventCheck(KEYEVENT_MOUSE_DOWN, code); }
8535a3
int mouseWentDown(const char *code)
1015c5
	{ return keyEventCheck(KEYEVENT_MOUSE_WENTDOWN, code); }
8535a3
int mouseWentUp(const char *code)
1015c5
	{ return keyEventCheck(KEYEVENT_MOUSE_WENTUP, code); }
3f9996
double mouseX()
3f9996
	{ return _mouseX; }
3f9996
double mouseY()
3f9996
	{ return _mouseY; }
3f9996
3f9996
int mousePressedOver(Sprite sprite)
1015c5
	{ return keyEventGetCount(KEYEVENT_MOUSE_DOWN) && mouseIsOver(sprite); }
3f9996
981405
static void resize(int w, int h) {
a20939
	w = w > 200 ? w : 100;
a20939
	h = h > 200 ? h : 200;
59dae5
	if (width != w || height != h) {
a20939
		width = w;
a20939
		height = h;
a20939
		if (started && !stopped && window)
a20939
			SDL_SetWindowSize(window, width, height);
981405
	}
981405
}
981405
	
3f9996
int worldGetWidth()
3f9996
	{ return width; }
981405
void worldSetWidth(int w)
981405
	{ resize(w, height); }
3f9996
07b70f
int worldGetHeight()
3f9996
	{ return height; }
981405
void worldSetHeight(int h)
981405
	{ resize(width, h); }
3f9996
8eb855
int worldGetResizable()
8eb855
	{ return resizable; }
8eb855
void worldSetResizable(int r) {
8eb855
	if (resizable == r) return;
8eb855
	resizable = r ? TRUE : FALSE;
8eb855
	if (started && !stopped && window)
a20939
		SDL_SetWindowResizable(window, resizable);
8eb855
}
8eb855
8eb855
const char* worldGetTitle()
8eb855
	{ return title; }
8eb855
void worldSetTitle(const char *t) {
8eb855
	int changed = FALSE;
8eb855
	for(int i = 0; i < titleSize-1; ++i) {
8eb855
		if (title[i] != t[i]) changed = TRUE;
8eb855
		title[i] = t[i];
8eb855
		if (!t[i]) break;
8eb855
	}
8eb855
	if (changed && started && !stopped && window)
a20939
		SDL_SetWindowTitle(window, title);
8eb855
}
8eb855
3f9996
double worldGetFrameRate()
3f9996
	{ return frameRate; }
e034e4
void worldSetFrameRate(double fps) {
e034e4
	if (!(fps > 1)) fps = 1;
e034e4
	if (!(fps < 100)) fps = 100;
e034e4
	frameRate = fps;
3f9996
}
3f9996
981405
double worldGetTimeStep()
981405
	{ return 1/frameRate; }
981405
3f9996
int worldGetFrameCount()
3f9996
	{ return (int)frameCount; }
3f9996
double worldGetSeconds()
a20939
	{ return started ? elapsedTimeUs*1e-6 : 0.0; }
3f9996
3f9996
void worldSetInit(Callback init)
3f9996
	{ initCallback = init; }
3f9996
void worldSetDraw(Callback draw)
3f9996
	{ drawCallback = draw; }
1015c5
void worldSetDeinit(Callback deinit)
1015c5
	{ deinitCallback = deinit; }
3f9996
void worldStop()
3f9996
	{ if (started) stopped = TRUE; }
3f9996
3f9996
3f9996
void cameraOn()
3f9996
	{ cameraEnabled = TRUE; }
3f9996
void cameraOff()
3f9996
	{ cameraEnabled = FALSE; }
3f9996
int cameraIsActive()
3f9996
	{ return cameraEnabled; }
3f9996
3f9996
double cameraMouseX()
3f9996
	{ return _mouseX/cameraZoom + cameraX; }
3f9996
double cameraMouseY()
3f9996
	{ return _mouseY/cameraZoom + cameraY; }
3f9996
3f9996
double cameraGetX()
3f9996
	{ return cameraX; }
3f9996
void cameraSetX(double x)
3f9996
	{ cameraX = x; }
3f9996
3f9996
double cameraGetY()
3f9996
	{ return cameraY; }
3f9996
void cameraSetY(double y)
3f9996
	{ cameraY = y; }
3f9996
3f9996
double cameraGetZoom()
3f9996
	{ return cameraZoom; }
3f9996
void cameraSetZoom(double x)
1c7488
	{ cameraZoom = x > HELI_PRECISION ? x : HELI_PRECISION; }
3f9996
1015c5
3954ba
void messageBox(const char *message) {
3954ba
	SDL_ShowSimpleMessageBox(
3954ba
		SDL_MESSAGEBOX_INFORMATION,
3954ba
		title,
3954ba
		message,
3954ba
		window );
3954ba
}
3954ba
3954ba
a20939
static void draw() {
a20939
	unsigned int currentFrameTimeMs = SDL_GetTicks();
a20939
	unsigned long long deltaUs = frameCount == 0 ? 0 : (currentFrameTimeMs - prevFrameTimeMs)*1000ull;
a20939
	prevFrameTimeMs = currentFrameTimeMs;
d1f083
	
981405
	double dt = 1/frameRate;
a20939
	unsigned long long timeStepUs = (unsigned long long)round(dt*1e6);
a20939
	elapsedTimeSinceLastFrameUs += deltaUs;
a20939
	if (elapsedTimeSinceLastFrameUs > 2000000)
a20939
		elapsedTimeSinceLastFrameUs = 2000000;
a20939
	if (frameCount == 0 || elapsedTimeSinceLastFrameUs >= timeStepUs) {
a20939
		elapsedTimeUs += timeStepUs;
a20939
		if (frameCount != 0) elapsedTimeSinceLastFrameUs -= timeStepUs;
981405
		++frameCount;
a20939
		
981405
		heliSpriteUpdate(dt);
09c823
		heliSoundUpdate();
650f35
		
a20939
		glViewport(0, 0, width, height);
a20939
		glMatrixMode(GL_PROJECTION);
a20939
		glLoadIdentity();
a20939
		glOrtho(0, width, height, 0, -1.0, 1.0);
a20939
		glMatrixMode(GL_MODELVIEW);
a20939
		
a20939
		heliDrawingPrepareFrame();
a20939
		if (drawCallback) drawCallback();
a20939
		
1015c5
		heliArrayClear(&keyEvents[KEYEVENT_KEY_WENTDOWN]);
1015c5
		heliArrayClear(&keyEvents[KEYEVENT_KEY_WENTUP]);
1015c5
		heliArrayClear(&keyEvents[KEYEVENT_MOUSE_WENTDOWN]);
1015c5
		heliArrayClear(&keyEvents[KEYEVENT_MOUSE_WENTUP]);
650f35
		
a20939
		SDL_GL_SwapWindow(window);
3f9996
	}
650f35
	
a20939
	unsigned long long addUs = elapsedTimeSinceLastFrameUs + (SDL_GetTicks() - prevFrameTimeMs)*1000ull;
a20939
	if (addUs < timeStepUs) {
a20939
		unsigned long long waitUs = timeStepUs - addUs;
a20939
		if (waitUs > 2000)
a20939
			SDL_Delay( (unsigned int)((waitUs + 500)/1000ull) );
650f35
	}
a20939
}
a20939
a20939
a20939
static void deinit() {
a20939
	if (context) SDL_GL_DeleteContext(context);
a20939
	context = NULL;
650f35
	
a20939
	if (window) SDL_DestroyWindow(window);
a20939
	window = NULL;
650f35
	
a20939
	if (sdlInitialized) SDL_Quit();
a20939
	sdlInitialized = FALSE;
a20939
}
a20939
a20939
a20939
static int init() {
a20939
	if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
3954ba
		fprintf(stderr, "helianthus: SDL_Init failed\n");
a20939
		deinit();
a20939
		return FALSE;
a20939
	}
a20939
	sdlInitialized = TRUE;
a20939
	
a20939
	SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
a20939
	SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
b9c036
	SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
ba9f06
	SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
a20939
	SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
a20939
	SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 8);
a20939
	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
a20939
	SDL_GL_SetSwapInterval(1);
a20939
	
a20939
	unsigned int flags = SDL_WINDOW_OPENGL;
a20939
	if (resizable) flags |= SDL_WINDOW_RESIZABLE;
a20939
	window = SDL_CreateWindow(
a20939
		title,
a20939
		SDL_WINDOWPOS_CENTERED,
a20939
		SDL_WINDOWPOS_CENTERED,
a20939
		width,
a20939
		height,
a20939
		flags );
a20939
	
a20939
	if (!window) {
3954ba
		fprintf(stderr, "helianthus: cannot create window: %s\n", SDL_GetError());
a20939
		SDL_ClearError();
a20939
		deinit();
a20939
		return FALSE;
a20939
	}
a20939
	
a20939
	context = SDL_GL_CreateContext(window);
a20939
	if (!context) {
3954ba
		fprintf(stderr, "helianthus: cannot create OpenGL context: %s\n", SDL_GetError());
a20939
		SDL_ClearError();
a20939
		deinit();
a20939
		return FALSE;
e034e4
	}
650f35
	
3f9996
	return TRUE;
3f9996
}
3f9996
a20939
a20939
static void handleEvent(SDL_Event *e) {
a20939
	if (e->type == SDL_QUIT) {
a20939
		stopped = TRUE;
a20939
	} else
a20939
	if (e->type == SDL_WINDOWEVENT) {
a20939
		if (e->window.event == SDL_WINDOWEVENT_CLOSE) {
a20939
			stopped = TRUE;
a20939
		} else
a20939
		if (e->window.event == SDL_WINDOWEVENT_RESIZED) {
a20939
			width = e->window.data1;
a20939
			height = e->window.data2;
a20939
		} else
a20939
		if (e->window.event == SDL_WINDOWEVENT_FOCUS_LOST) {
1015c5
			int count = keyEventGetCount(KEYEVENT_KEY_DOWN);
1015c5
			for(int i = 0; i < count; ++i)
1015c5
				keyEventAdd(KEYEVENT_KEY_WENTUP, keyEventGet(KEYEVENT_KEY_DOWN, i));
1015c5
			heliArrayClear(&keyEvents[KEYEVENT_KEY_DOWN]);
a20939
			
a20939
			count = keyEventGetCount(KEYEVENT_MOUSE_DOWN);
a20939
			for(int i = 0; i < count; ++i)
a20939
				keyEventAdd(KEYEVENT_MOUSE_WENTUP, keyEventGet(KEYEVENT_MOUSE_DOWN, i));
a20939
			heliArrayClear(&keyEvents[KEYEVENT_MOUSE_DOWN]);
1015c5
		}
a20939
	} else
a20939
	if (e->type == SDL_KEYDOWN || e->type == SDL_KEYUP) {
a20939
		const char *keynameOrig = SDL_GetKeyName(e->key.keysym.sym);
a20939
		if (keynameOrig && *keynameOrig) {
a20939
			char *keyname = heliStringCopy(keynameOrig);
a20939
			heliLowercase(keyname);
a20939
			if (e->type == SDL_KEYDOWN) {
a20939
				keyEventAdd(KEYEVENT_KEY_DOWN, keyname);
a20939
				keyEventAdd(KEYEVENT_KEY_WENTDOWN, keyname);
a20939
			} else {
a20939
				keyEventRemove(KEYEVENT_KEY_DOWN, keyname);
a20939
				keyEventAdd(KEYEVENT_KEY_WENTUP, keyname);
a20939
			}
a20939
			free(keyname);
a20939
		}
a20939
	} else
a20939
	if (e->type == SDL_MOUSEBUTTONDOWN || e->type == SDL_MOUSEBUTTONUP) {
a20939
		char *button = NULL;
a20939
		switch(e->button.button) {
a20939
			case SDL_BUTTON_LEFT:   button = "left";   break;
a20939
			case SDL_BUTTON_MIDDLE: button = "middle"; break;
a20939
			case SDL_BUTTON_RIGHT:  button = "right";  break;
a20939
			default: break;
a20939
		}
a20939
		if (button) {
a20939
			if (e->type == SDL_MOUSEBUTTONDOWN) {
a20939
				keyEventAdd(KEYEVENT_MOUSE_DOWN, button);
a20939
				keyEventAdd(KEYEVENT_MOUSE_WENTDOWN, button);
a20939
			} else {
a20939
				keyEventRemove(KEYEVENT_MOUSE_DOWN, button);
a20939
				keyEventAdd(KEYEVENT_MOUSE_WENTUP, button);
a20939
			}
a20939
		}
a20939
		_mouseX = e->button.x;
a20939
		_mouseY = e->button.y;
a20939
	} else
a20939
	if (e->type == SDL_MOUSEMOTION) {
a20939
		_mouseX = e->motion.x;
a20939
		_mouseY = e->motion.y;
3f9996
	}
a20939
	
3f9996
}
3f9996
a20939
a20939
static void run() {
a20939
	while(!stopped) {
a20939
		SDL_Event event;
a20939
		while (SDL_PollEvent(&event))
a20939
			handleEvent(&event);
a20939
		draw();
a20939
	}
3f9996
}
3f9996
a20939
8eb855
void worldRun() {
3f9996
	if (started) return;
3f9996
	started = TRUE;
3f9996
	stopped = FALSE;
a20939
	
3f9996
	frameCount = 0;
a20939
	elapsedTimeUs = 0;
a20939
	elapsedTimeSinceLastFrameUs = 0;
3f9996
	srand(time(NULL));
1015c5
	
a20939
	if (init()) {
a20939
		heliInitialized = TRUE;
a20939
		
a20939
		if (initCallback) initCallback();
a20939
		run();
a20939
		if (deinitCallback) deinitCallback();
a20939
		
a20939
		heliArrayClear(&heliObjectsSet);
a20939
		heliSpriteFinish();
a20939
		heliDrawingFinish();
a20939
		heliFontFinish();
a20939
		heliAnimationFinish();
a20939
		heliSoundFinish();
a20939
		heliArrayDestroy(&heliObjectsSet);
a20939
		
a20939
		heliInitialized = FALSE;
a20939
		
a20939
		deinit();
a20939
		
a20939
		for(int i = 0; i < keyEventsCount; ++i)
a20939
			heliArrayDestroy(&keyEvents[i]);
a20939
	}
3f9996
	
3f9996
	started = FALSE;
3f9996
}