11 frameCount = lastTime = 0;capLast = capCur = 0;
16 gs.setGameState(gs.SPLASH);
21 SDL_DestroyRenderer(engren);
22 SDL_DestroyWindow(engwin);
26 SDL_Renderer*
Engine::init(
string s,
const int& w,
const int& h,
int flag) {
27 init(s, w, h, flag, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SDL_INIT_EVERYTHING);
30 SDL_Renderer*
Engine::init(
string s,
const int& w,
const int& h,
int flag,
int it) {
31 init(s, w, h, flag, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, it);
34 SDL_Renderer*
Engine::init(
string s,
const int& w,
const int& h,
int flag,
int x,
int y) {
35 init(s, w, h, flag, x, y, SDL_INIT_EVERYTHING);
38 SDL_Renderer*
Engine::init(
string s,
const int& w,
const int& h,
int flag,
int x,
int y,
int it) {
39 int res = SDL_Init(it);
40 SDL_CHECK(res == 0,
"SDL_Init");
42 SDL_CreateWindowAndRenderer(WIDTH, HEIGHT, flag, &engwin, &engren);
43 SDL_CHECK(engwin,
"SDL_CreateWindowAndRenderer");
44 SDL_CHECK(engren,
"SDL_CreateWindowAndRenderer");
50 SDL_GL_CreateContext(engwin);
51 glMatrixMode(GL_PROJECTION);
53 gluPerspective(70,(
double)w/h,1,1000);
54 glEnable(GL_DEPTH_TEST);
58 SDL_SetRenderDrawColor(engren, sr, sg, sb, 255);
63 SDL_RenderFillRect(engren, &rect);
67 o.setDestSize(256,256);
71 SDL_RenderPresent(engren);
79 SDL_SetWindowTitle(engwin, s.c_str());
82 SDL_SetWindowPosition(engwin, x, y);
100 if(!glMode) SDL_RenderClear(engren);
101 SDL_SetRenderDrawColor(engren, red, green, blue, 0xff);
103 lastFrame = getTicks();
105 if(curTime > lastTime) {
106 lastTime = curTime; curFPS=frameCount; cout <<
"FPS: " << curFPS << endl; frameCount=0;
114 realTime = gettimeofday(&a, 0);
122 SDL_GL_SwapWindow(engwin);
124 SDL_RenderPresent(engren);
128 int timerFps = getTicks() - lastFrame;
129 if(timerFps < 1000/setFPS) {
130 delay((1000/setFPS) - timerFps);
138 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
139 glMatrixMode( GL_MODELVIEW );
141 gluLookAt(glView[0],glView[1],glView[2],glView[3],glView[4],glView[5],glView[6],glView[7],glView[8]);
145 void Engine::loop() {
152 if(gs.getGameState()!=0) {
158 SDL_SetRenderDrawColor(engren, obj.getColor().r, obj.getColor().g, obj.getColor().b, obj.getColor().a);
159 SDL_RenderFillRect(engren, &des);
160 SDL_SetRenderDrawColor(engren, red, green, blue, 255);
165 if(gs.getGameState()!=0) {
166 for(
int i=0; i<objs.size(); i++) {
172 if(gs.getGameState()!=0) {
173 for(
int i=0; i<objs.size(); i++) {
186 if(gs.getGameState()!=0) {
188 for(
int i=0; i<tiles.size(); i++) {
192 for(
int i=0; i<objects.size(); i++) {
196 for(
int i=0; i<entities.size(); i++) {
202 void Engine::loadFont(
char *font_path) {
203 font = TTF_OpenFont(font_path, 24);
206 void Engine::draw(
const char *text,
int x,
int y,
int r,
int g,
int b) {
207 SDL_Surface *surface;
208 SDL_Texture *texture;
211 fprintf(stderr,
"error: font not found\n");
220 surface = TTF_RenderText_Solid(font, text, color);
221 texture = SDL_CreateTextureFromSurface(engren, surface);
226 SDL_FreeSurface(surface);
227 SDL_RenderCopy(engren, texture, NULL, &rect);
228 SDL_DestroyTexture(texture);
249 SDL_RenderPresent(engren);
260 if(gs.getGameState() == 0) {
271 cout <<
"In debug mode!" << endl;
272 cout <<
"Which at this time only means bypassing the splashscreen." << endl;
275 void Engine::hideMouse() {
276 SDL_ShowCursor(
false);
279 void Engine::showMouse() {
280 SDL_ShowCursor(
true);
283 void Engine::setGLView(
int a,
int b,
int c,
int d,
int e,
int f,
int g,
int h,
int i) {
284 glView[0]=a; glView[1]=b; glView[2]=c; glView[3]=d; glView[4]=e; glView[5]=f; glView[6]=g; glView[7]=h; glView[8]=i;
287 int Engine::getTicks() {
return SDL_GetTicks(); }
288 void Engine::delay(
int time) { SDL_Delay(time); }
This class stores information for an Object in the game.
void setName(string s)
Set window name.
void bypassSplash(int key)
Deactives the splashscreen, requires key.
void setImage(Image i)
Set the Object's Image with a given Image.
void draw(Object obj)
Draw an object on the screen.
vector< Object > getObjectsToRender()
Return the Objects that are currently on the screen.
SDL_Renderer * getRenderer()
Returns screen renderer.
void setBackground(Background b)
Set background.
void setSize(int w, int y)
Set window size.
void setFrame(SDL_Rect i)
Set the frame with a given SDL_Rect.
void splash()
Calls splashscreen at the beginning of the game. This is automatically called unless deactivated...
vector< Entity > getEntitiesToRender()
Return the Entities that are currently on the screen.
void drawBackground()
Draw background.
SDL_Texture * getTexture()
Get SDL_Texture.
void loopStart()
Call this at the beginning of a loop to initilaize the loop.
SDL_Rect getDest() const
Get the destination for the Object to be displayed on screen.
~Engine()
Decontructs renderer and window and then quits SDL.
void setPos(int x, int y)
Set window position.
vector< Tile > getTilesToRender()
Return the Tiles that are currently on the screen.
Object that is a background image that covers the screen.
double getAngle() const
Get the Object's angle.
void setBackground(string file, int w, int h, SDL_Renderer *ren)
Sets the background with a path to the file name, the width and height of the screen, and the renderer.
Image getImage() const
Get the Object's Image.
void center(int w, int h)
Center the Object based on a width and height.
void debugMode(bool d)
Active debugger with Boolean.
bool hasSplashed()
Check if the splashscreen has occured.
This class stores a Stage and Objects and can move them and display them.
SDL_Renderer * init(string s, const int &w, const int &h, int flag)
Create a window with a given name, width, height, and anyother SDL_Window flags.
SDL_Rect getFrame() const
Get the frame that the Object parses from the Image.
void render()
Call this at the end of the game loop to render.
void setColor(Uint8 r, Uint8 g, Uint8 b)
Sets SDL color.