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.
 
 
 
 
 
 

170 lines
3.8 KiB

#include <stdio.h>
#include <list>
#include <SDL2/SDL.h>
#include <SDL2/SDL2_gfxPrimitives.h>
#include <SDL2/SDL_image.h>
#include "Timer.h"
#include "Texture.h"
#include "Character.h"
#include "NPC.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;
std::list<Character> characters;
std::list<NPC> npcs;
int main(int argc, char** argv) {
init();
Map map;
map.loadFromFile("assets/Cave.png");
L_W = map.getW();
L_H = map.getH();
printf("Width: %d, Height: %d\n",L_W,L_H);
Character ch(1,"assets/sp.png",0,0);
characters.push_back(ch);
printf("Constructing char 2\n");
Character ch2(2,"assets/jj.png",128,0);
printf("Pushing back char 2\n");
characters.push_back(ch2);
printf("Char 2 included! \n");
auto active = characters.begin();
NPC box(64,64,"assets/box.png");
npcs.push_back(box);
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;
}
}
active->handleEvent(e);
}
active->move(map.getTiles());
SDL_RenderClear(renderer);
map.render(active->getCamera());
active->render(active->getCamera());
active->renderHUD();
for(auto itr = characters.begin(); itr != characters.end(); ++itr) {
if(itr != active) {
itr->render(active->getCamera());
}
}
for(auto itr = npcs.begin(); itr != npcs.end(); ++itr) {
itr->render(active->getCamera());
}
SDL_RenderPresent(renderer);
if(!active->isTurn()) {
active->resetTurn();
active++;
if(active == characters.end()){
//printf("Back to beginning\n");
active = characters.begin();
}
else{
//printf("Next character\n");
}
active->startTurn();
//printf("Next turn!\n");
}
//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();
}