binding-of-isaac/src/main.c

176 lines
4.8 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>
#include <stdbool.h>
#include <ncurses.h>
#include <unistd.h>
#include <termios.h>
#include <limits.h>
#include <time.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include "hash.h"
#include "structure.h"
#include "base.h"
#include "move.h"
#include "triangles.h"
#include "proj.h"
#include "entities.h"
#include "display.h"
#include "generation.h"
double sim_time ;
int main(int argc, char** argv) {
srand(time(NULL));
//-------------------------------------------------------------------------------//
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
printf( "error initializing SDL: %s\n", SDL_GetError());
}
SDL_Window* win = SDL_CreateWindow("Game",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
1500, 1000, 0);
Uint32 render_flags = SDL_RENDERER_ACCELERATED;
SDL_Renderer* rend = SDL_CreateRenderer(win, -1, render_flags);
if(rend == NULL) {
printf( "ERROR : cannot initialize SDL renderer\n");
exit(1);
}
printf( "%d\n", SDL_SetRenderDrawBlendMode(rend, SDL_BLENDMODE_BLEND));
//-------------------------------------------------------------------------------//
if(SDL_Init(SDL_INIT_AUDIO)) {
fprintf(stderr, "cannot initialize audio");
exit(1);
}
SDL_SetRelativeMouseMode(true) ;
/* -------------------------------------------------------- */
int fps = 60;
int interval = 1000000/fps;
double intervalf = 1.0/((double)(fps));
bool debug_main = true ;
init_csts();
init_hashtbl();
init_draworder();
init_ent_generator(10);
trInit();
init_proj();
parse_rooms(5);
import_digits(rend) ;
import_letters(rend) ;
sim_time = 0.0 ;
clock_t origin = clock();
clock_t finish = clock();
clock_t entstart = clock();
clock_t entend = clock();
float delta;
while(!stop_evetything) {
resetRenderer(rend) ;
origin = clock();
SDL_SetRenderDrawColor(rend, 255, 255, 255, SDL_ALPHA_OPAQUE) ;
entend = clock();
//printf("00\n");
//printf("%s\n", SDL_GetError());
//fflush(stdout);
playerActions(((float)entend - (float)entstart)/CLOCKS_PER_SEC) ;
//printf("01\n");
//fflush(stdout);
generate_nearby_chunks(1);
//printf("02\n");
//fflush(stdout);
entend = clock();
update_entities(((float)entend - (float)entstart)/CLOCKS_PER_SEC);
updateAllProj(((float)entend - (float)entstart)/CLOCKS_PER_SEC);
//printf("03\n");
//fflush(stdout);
entstart = clock();
//printf("-->%d\n", triangles_i);
drawCurrentRoom(rend);
//printf("-->%d\n", triangles_i);
//printf("-->%d\n", triangles_i);
//printf("04\n");
//printf("%s\n", SDL_GetError());
//fflush(stdout);
drawData(rend) ;
//printf("05\n");
//fflush(stdout);
drawHPbar(rend);
//printf("06\n");
//fflush(stdout);
finish = clock();
fade_dmg = max(fade_dmg-5, 0);
delta = ((float)finish - (float)origin)/CLOCKS_PER_SEC;
//printf("07\n");
//fflush(stdout);
drawNumberToRenderer(rend, digits, (int)(1.0f/delta), 720, 60, 75/2, 105/2, 0);
drawNumberToRenderer(rend, digits, (int)(10*sim_time), 720, 110, 75/2, 105/2, 0);
drawNumberToRenderer(rend, digits, coins, 1500/2-55, 1000 - 70, 75/3, 105/3, 0);
//printf("08\n");
//fflush(stdout);
//printf("%s\n", SDL_GetError());
updateRenderer(rend) ;
sim_time += delta + intervalf ;
//printf("09\n");
//fflush(stdout);
if(player_hp <= 0) {
stop_evetything = true ;
}
usleep(interval) ;
}
//printf("GPNE\n");
//fflush(stdout);
free_digits(digits) ;
for(int k = 0; k < MAX_SIZE; k++) {
free(triangles_to_render[k]);
free(triangles_og_coords[k]);
}
free(drawOrder);
free(triangles_to_render);
free(triangles_og_coords);
free(reds);
free(greens);
free(blues);
free(triangles_order);
free(triangles_shr);
free(visited_tri);
free_proj();
//printf("10\n");
//fflush(stdout);
hashtbl_free(visited);
free_pool();
/* -------------------------------------------------------- */
SDL_DestroyRenderer(rend);
//printf("10\n");
//fflush(stdout);
SDL_DestroyWindow(win);
//printf("11\n");
//fflush(stdout);
SDL_Quit();
//printf("12\n");
//fflush(stdout);
/* -------------------------------------------------------- */
//printf("Done\n") ;
//fflush(stdout);
return 0;
}