breakhack/src/main.c

204 lines
4.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"
2017-11-30 21:00:47 +01:00
#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-12-05 08:30:08 +01:00
#include "timer.h"
#include "roommatrix.h"
2017-11-30 21:00:47 +01:00
static SDL_Window *gWindow = NULL;
static SDL_Renderer *gRenderer = NULL;
static Player *gPlayer = NULL;
2017-11-30 21:00:47 +01:00
static LinkedList *gSpriteList = NULL;
2017-12-01 16:03:19 +01:00
static Map *gMap = NULL;
static RoomMatrix *gRoomMatrix = NULL;
2017-12-01 16:03:19 +01:00
static Camera gCamera;
2017-11-30 21:00:47 +01:00
static
bool initSDL(void)
2017-11-30 21:00:47 +01:00
{
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;
2017-12-15 08:08:45 +01:00
if (dim.height > 768) {
2017-11-30 21:00:47 +01:00
printf("[**] Hi resolution screen detected (%u x %u)\n", dim.width, dim.height);
scale = ((double) dim.height)/1080;
2017-11-30 21:00:47 +01:00
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,
(int)(SCREEN_WIDTH * scale),
(int)(SCREEN_HEIGHT * scale),
2017-11-30 21:00:47 +01:00
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;
}
2017-12-10 19:51:24 +01:00
if (SDL_SetRenderDrawBlendMode(gRenderer, SDL_BLENDMODE_BLEND) < 0) {
printf("[!!] Unable to set blend mode: %s\n", SDL_GetError());
return false;
}
2017-11-30 21:00:47 +01:00
if (SDL_RenderSetLogicalSize(gRenderer, SCREEN_WIDTH, SCREEN_HEIGHT) < 0)
{
2017-12-15 15:03:29 +01:00
printf("[!!] Unable to initiate scaling: %s\n",
SDL_GetError());
2017-11-30 21:00:47 +01:00
return false;
}
if ( (IMG_Init(imgFlags) & imgFlags) == 0 ) {
2017-12-15 15:03:29 +01:00
printf("[!!] Unable to initiate img loading: %s\n",
IMG_GetError());
return false;
}
if ( TTF_Init() == -1 ) {
printf("[!!] Unable to initiate ttf library: %s\n",
TTF_GetError());
2017-11-30 21:00:47 +01:00
return false;
}
return true;
}
static
bool initGame(void)
2017-11-30 21:00:47 +01:00
{
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(void)
2017-11-30 21:00:47 +01:00
{
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;
gRoomMatrix = roommatrix_create();
2017-12-01 16:03:19 +01:00
}
2017-12-17 13:43:41 +01:00
2017-11-30 21:00:47 +01:00
return result;
}
static
void loadMedia(void)
2017-11-30 21:00:47 +01:00
{
gPlayer = player_create(ROGUE, gRenderer);
2017-11-30 21:00:47 +01:00
}
2017-12-03 11:09:57 +01:00
static
bool handle_events(void)
2017-12-03 11:09:57 +01:00
{
static SDL_Event event;
bool quit = false;
while (SDL_PollEvent(&event) != 0) {
if (event.type == SDL_QUIT) {
quit = true;
} else {
2017-12-15 08:08:45 +01:00
gPlayer->handle_event(gPlayer,
gRoomMatrix,
&event);
camera_follow_position(&gCamera, &gPlayer->sprite->pos);
map_set_current_room(gMap, &gPlayer->sprite->pos);
2017-12-03 11:09:57 +01:00
}
}
return quit;
}
2017-11-30 21:00:47 +01:00
static
void run(void)
2017-11-30 21:00:47 +01:00
{
bool quit = false;
2017-12-05 08:30:08 +01:00
Timer* fpsTimer = timer_create();
2017-11-30 21:00:47 +01:00
while (!quit)
{
2017-12-05 08:30:08 +01:00
timer_start(fpsTimer);
2017-11-30 21:00:47 +01:00
2017-12-03 11:09:57 +01:00
quit = handle_events();
2017-12-15 08:08:45 +01:00
map_clear_dead_monsters(gMap);
roommatrix_populate_from_map(gRoomMatrix, gMap);
2017-12-10 19:51:24 +01:00
roommatrix_add_lightsource(gRoomMatrix,
&gPlayer->sprite->pos);
2017-12-10 19:51:24 +01:00
roommatrix_build_lightmap(gRoomMatrix);
2017-11-30 21:00:47 +01:00
2017-12-17 13:43:41 +01:00
if (gPlayer->steps == gPlayer->stats.speed) {
player_reset_steps(gPlayer);
roommatrix_update_with_player(gRoomMatrix, gPlayer);
2017-12-17 13:43:41 +01:00
map_move_monsters(gMap, gRoomMatrix);
}
2017-11-30 21:00:47 +01:00
SDL_RenderClear(gRenderer);
2017-12-01 16:03:19 +01:00
map_render(gMap, &gCamera);
player_render(gPlayer, &gCamera);
2017-12-10 19:51:24 +01:00
roommatrix_render_lightmap(gRoomMatrix, &gCamera);
2017-11-30 21:00:47 +01:00
SDL_RenderPresent(gRenderer);
2017-12-05 08:30:08 +01:00
int ticks = timer_get_ticks(fpsTimer);
2017-11-30 21:00:47 +01:00
if (ticks < 1000/60)
SDL_Delay((1000/60) - ticks);
2017-12-05 08:30:08 +01:00
timer_stop(fpsTimer);
2017-11-30 21:00:47 +01:00
}
2017-12-05 08:30:08 +01:00
timer_destroy(fpsTimer);
2017-11-30 21:00:47 +01:00
}
static
void close(void)
2017-11-30 21:00:47 +01:00
{
player_destroy(gPlayer);
2017-12-01 16:03:19 +01:00
map_destroy(gMap);
roommatrix_destroy(gRoomMatrix);
2017-11-30 21:00:47 +01:00
SDL_DestroyWindow(gWindow);
gWindow = NULL;
TTF_Quit();
2017-11-30 21:00:47 +01:00
IMG_Quit();
SDL_Quit();
}
int main(int argc, char *argv[])
2017-11-30 21:00:47 +01:00
{
UNUSED(argc);
UNUSED(argv);
2017-11-30 21:00:47 +01:00
if (!init())
return 1;
loadMedia();
run();
close();
return 0;
}