breakhack/src/main.c

168 lines
3.1 KiB
C
Raw Normal View History

2017-11-30 21:00:47 +01:00
#include <stdio.h>
#include <stdbool.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <linkedlist.h>
#include "player.h"
#include "screenresolution.h"
#include "dimension.h"
2017-12-01 16:03:19 +01:00
#include "camera.h"
#include "map.h"
2017-12-02 23:32:40 +01:00
#include "map_lua.h"
2017-11-30 21:00:47 +01:00
#define SCREEN_WIDTH 1024
#define SCREEN_HEIGHT 768
static SDL_Window *gWindow = NULL;
static SDL_Renderer *gRenderer = NULL;
static Sprite *gPlayer = NULL;
static LinkedList *gSpriteList = NULL;
2017-12-01 16:03:19 +01:00
static Map *gMap = NULL;
static Camera gCamera;
2017-11-30 21:00:47 +01:00
static
bool initSDL()
{
int imgFlags = IMG_INIT_PNG;
2017-12-03 11:09:57 +01:00
Dimension dim = getScreenDimensions();
//Dimension dim = (Dimension) { 1920, 1080 };
2017-11-30 21:00:47 +01:00
double scale = 1.0;
if (dim.height > 1080) {
printf("[**] Hi resolution screen detected (%u x %u)\n", dim.width, dim.height);
scale = ((double) dim.height)/1080;
printf("[**] Scaling by %f\n", scale);
}
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
printf("[!!] Could not initiate SDL2: %s\n", SDL_GetError());
return false;
}
gWindow = SDL_CreateWindow("Breakhack",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH * scale,
SCREEN_HEIGHT * scale,
SDL_WINDOW_SHOWN);
if (gWindow == NULL)
{
printf("[!!] Unable to create window: %s\n", SDL_GetError());
return false;
}
gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED);
if (gRenderer == NULL)
{
printf("[!!] Unable to create renderer: %s\n", SDL_GetError());
return false;
}
if (SDL_RenderSetLogicalSize(gRenderer, SCREEN_WIDTH, SCREEN_HEIGHT) < 0)
{
printf("[!!] Unable to initiate scaling: %s\n", SDL_GetError());
return false;
}
if ( (IMG_Init(imgFlags) & imgFlags) == 0 ) {
printf("[!!] Unable to initiate img loading: %s\n", IMG_GetError());
return false;
}
return true;
}
static
bool initGame()
{
gSpriteList = linkedlist_create();
2017-12-02 23:32:40 +01:00
gMap = map_lua_generator_run(gRenderer);
2017-11-30 21:00:47 +01:00
return gSpriteList == NULL;
}
static
bool init()
{
bool result = true;
result = result && initSDL();
result = result && initGame();
2017-12-01 16:03:19 +01:00
if (result) {
gCamera.pos = (Position) { 0, 0 };
gCamera.renderer = gRenderer;
}
2017-11-30 21:00:47 +01:00
return result;
}
static
void loadMedia()
{
gPlayer = player_create(WARRIOR, gRenderer);
}
2017-12-03 11:09:57 +01:00
static
bool handle_events()
{
static SDL_Event event;
bool quit = false;
while (SDL_PollEvent(&event) != 0) {
if (event.type == SDL_QUIT) {
quit = true;
} else {
gPlayer->handle_event(gPlayer, &event);
camera_follow_position(&gCamera, &gPlayer->pos);
map_set_current_room(gMap, &gPlayer->pos);
}
}
return quit;
}
2017-11-30 21:00:47 +01:00
static
void run()
{
bool quit = false;
while (!quit)
{
2017-12-01 00:01:51 +01:00
int ticks = SDL_GetTicks();
2017-11-30 21:00:47 +01:00
2017-12-03 11:09:57 +01:00
quit = handle_events();
2017-11-30 21:00:47 +01:00
SDL_RenderClear(gRenderer);
2017-12-01 16:03:19 +01:00
map_render(gMap, &gCamera);
sprite_render(gPlayer, &gCamera);
2017-11-30 21:00:47 +01:00
SDL_RenderPresent(gRenderer);
ticks = SDL_GetTicks() - ticks;
if (ticks < 1000/60)
SDL_Delay((1000/60) - ticks);
}
}
static
void close()
{
sprite_destroy(gPlayer);
2017-12-01 16:03:19 +01:00
map_destroy(gMap);
2017-11-30 21:00:47 +01:00
SDL_DestroyWindow(gWindow);
gWindow = NULL;
IMG_Quit();
SDL_Quit();
}
int main(int argc, char** argv)
{
if (!init())
return 1;
loadMedia();
run();
close();
return 0;
}