Contact me to be added to this repository. Push the creations you make for D&D, and they will be displayed in a nice website. See the website for how to contribute.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

155 lines
3.2 KiB

#include <stdio.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL2_gfxPrimitives.h>
#include <SDL2/SDL_image.h>
#include "Timer.h"
#include "Texture.h"
#include "Character.h"
#include "Map.h"
//screen width, height
const int S_W = 1280;
const int S_H = 960;
int L_W = -1;
int L_H = -1;
const int MAX_FPS = 60;
const int TICKS_PER_FRAME = 1000 / MAX_FPS;
const int TILE_SIZE = 64;
void init();
void close();
SDL_Window* window = NULL;
SDL_Renderer* renderer = NULL;
Timer fpsTimer;
Timer capTimer;
int frames_total;
int main(int argc, char** argv) {
init();
/*
Quad* tree = new Quad();
int size = 8;
tree->init(size,size);
for(int x = 0; x < size; ++x) {
for(int y = 0; y < size; ++y) {
tree->insert(x,y,static_cast<Tile>((x+y)/2));
}
}
for(int x = 0; x < size; ++x) {
for(int y = 0; y < size; ++y) {
printf("(%d,%d), expected %d and tile is %d\n",x,y,(x+y)/2,tree->lookup(x,y));
}
}
//tree->print(0);
tree->free();
//close();
//exit(1);
*/
Map map;
if(argc > 1) {
std::string s = argv[2];
map.loadFromFile(s);
} else {
map.loadFromFile("assets/Cave.png");
}
L_W = map.getW();
L_H = map.getH();
//map.print();
//exit(1);
printf("Width: %d, Height: %d\n",L_W,L_H);
Character ch("sp.png",0,0);
SDL_Event e;
bool done = false;
fpsTimer.start();
frames_total = 0;
while(!done) {
capTimer.start();
while(SDL_PollEvent(&e) != 0) {
if(e.type == SDL_QUIT) {
done = true;
break;
}
if(e.type == SDL_KEYDOWN) {
if(e.key.keysym.sym == SDLK_q) {
done = true;
break;
}
}
ch.handleEvent(e);
}
ch.move();
SDL_RenderClear(renderer);
map.render(ch.getCamera());
ch.render();
SDL_RenderPresent(renderer);
float avgFPS = frames_total / (fpsTimer.getTicks() / 1000.0f);
//printf("FPS: %f\n",avgFPS);
++frames_total;
int frameTicks = capTimer.getTicks();
if(frameTicks < TICKS_PER_FRAME) { //slow down there friend
SDL_Delay(TICKS_PER_FRAME-frameTicks);
}
}
close();
return 0;
}
#define CHECK(call) while(0) { \
if(call < 0) { \
printf("Error on %s, SDL_Error is: %s\n",#call,SDL_GetError()); \
exit(1);\
}\
}
#define CHECKNULL(call) while(0) { \
if(call == NULL) { \
printf("Error constructing %s, SDL_Error is: %s\n",#call,SDL_GetError()); \
exit(1);\
} \
}
void init() {
CHECK(SDL_Init(SDL_INIT_VIDEO));
CHECK(IMG_Init(IMG_INIT_PNG));
window = SDL_CreateWindow("A large but ultimately finite amount of bees",SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,S_W,S_H,SDL_WINDOW_SHOWN);
CHECKNULL(window);
renderer = SDL_CreateRenderer(window,-1,SDL_RENDERER_ACCELERATED);
CHECKNULL(renderer);
SDL_SetRenderDrawColor(renderer,0xFF,0xFF,0xFF,0xFF); //white with 255 alpha
}
void close() {
SDL_DestroyRenderer(renderer);
renderer = NULL;
SDL_DestroyWindow(window);
window = NULL;
IMG_Quit();
SDL_Quit();
}