Arch Game Engine  0.2
engine.cpp
1 #include "engine.h"
2 
3 Engine::Engine() {
4  //simulationTime = 0;
5  //realTime = 0;
6  setFPS = 60;
7  bkg = 0;
8  lastFrame = 0;
9  debug = false;
10  setRunning(true);
11  frameCount = lastTime = 0;capLast = capCur = 0;
12  //framesPerSecond = 60;
13  //capTime = 10;
14  //capMark = renderMiliGap = cappedFrame = 0;
15  glMode=false;
16  gs.setGameState(gs.SPLASH);
17  sr=sg=sb=22;
18 }
20  TTF_Quit();
21  SDL_DestroyRenderer(engren);
22  SDL_DestroyWindow(engwin);
23  SDL_Quit();
24 }
25 
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);
28  return engren;
29 }
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);
32  return engren;
33 }
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);
36  return engren;
37 }
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");
41  setSize(w, h);
42  SDL_CreateWindowAndRenderer(WIDTH, HEIGHT, flag, &engwin, &engren);
43  SDL_CHECK(engwin, "SDL_CreateWindowAndRenderer");
44  SDL_CHECK(engren, "SDL_CreateWindowAndRenderer");
45  setPos(x, y);
46  setName(s);
47  loopStart();
48  render();
49  if(glMode) {
50  SDL_GL_CreateContext(engwin);
51  glMatrixMode(GL_PROJECTION);
52  glLoadIdentity();
53  gluPerspective(70,(double)w/h,1,1000);
54  glEnable(GL_DEPTH_TEST);
55  }
56  TTF_Init();
57  if(!debug) {
58  SDL_SetRenderDrawColor(engren, sr, sg, sb, 255);
59  SDL_Rect rect;
60  rect.x=rect.y=0;
61  rect.w=WIDTH;
62  rect.h=HEIGHT;
63  SDL_RenderFillRect(engren, &rect);
64  Object o;
65  o.setImage("../../engine/res/engine-logo.png", getRenderer());
66  o.setFrame(0, 0, 256, 256);
67  o.setDestSize(256,256);
68  o.center(WIDTH, HEIGHT);
69  draw(o);
70  draw(o,4231998);
71  SDL_RenderPresent(engren);
72  SDL_Delay(2300);
73  gs.setGameState(1);
74  }
75  return engren;
76 }
77 
78 void Engine::setName(string s) {
79  SDL_SetWindowTitle(engwin, s.c_str());
80 }
81 void Engine::setPos(int x, int y) {
82  SDL_SetWindowPosition(engwin, x, y);
83 }
84 void Engine::setSize(int w, int h) {
85  WIDTH = w;
86  HEIGHT = h;
87 }
88 
89 SDL_Renderer* Engine::getRenderer() {
90  return engren;
91 }
92 
93 void Engine::setColor(Uint8 r, Uint8 g, Uint8 b) {
94  red = r;
95  green = g;
96  blue = b;
97 }
98 
100  if(!glMode) SDL_RenderClear(engren);
101  SDL_SetRenderDrawColor(engren, red, green, blue, 0xff);
102  if(bkg) drawBackground();
103  lastFrame = getTicks();
104  curTime = time(0);
105  if(curTime > lastTime) {
106  lastTime = curTime; curFPS=frameCount; cout << "FPS: " << curFPS << endl; frameCount=0;
107  }
108 }
110  //struct timespec spec; clock_gettime(CLOCK_REALTIME, &spec); capCur=round(spec.tv_nsec/1.0e6);
111  //if(renderMiliGap = 0 || (capCur-capLast)>renderMiliGap) {
112  //if(gs.getGameState() == 0) { cout << "splash" << endl; splash(); }
113  timeval a;
114  realTime = gettimeofday(&a, 0);
115  frameCount++;
116  //}
117  //capLast=capCur;
118 
119  if(glMode) {
120  glEnd();
121  glFlush();
122  SDL_GL_SwapWindow(engwin);
123  } else {
124  SDL_RenderPresent(engren);
125  }
126 
127 
128  int timerFps = getTicks() - lastFrame;
129  if(timerFps < 1000/setFPS) {
130  delay((1000/setFPS) - timerFps);
131  }
132 }
133 
135  //simulationTime += 16;
136  //if(simulationTime < realTime) { fps = true; } else { fps = false; }
137  if(glMode) {
138  glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
139  glMatrixMode( GL_MODELVIEW );
140  glLoadIdentity( );
141  gluLookAt(glView[0],glView[1],glView[2],glView[3],glView[4],glView[5],glView[6],glView[7],glView[8]);
142  }
143 }
144 
145 void Engine::loop() {
146  render();
147  loopStart();
148  update();
149 }
150 
151 void Engine::draw(Object obj) {
152  if(gs.getGameState()!=0) {
153  SDL_Rect des = obj.getDest();
154  if(obj.imageSet()) {
155  SDL_Rect src = obj.getFrame();
156  SDL_RenderCopyEx(engren, obj.getImage().getTexture(), &src, &des, obj.getAngle(), NULL, SDL_FLIP_NONE);
157  } else {
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);
161  }
162  }
163 }
164 void Engine::draw(vector<Object> objs) {
165  if(gs.getGameState()!=0) {
166  for(int i=0; i<objs.size(); i++) {
167  draw(objs[i]);
168  }
169  }
170 }
171 void Engine::draw(vector<vector<Object>> objs) {
172  if(gs.getGameState()!=0) {
173  for(int i=0; i<objs.size(); i++) {
174  draw(objs[i]);
175  }
176  }
177 }
178 void Engine::draw(Object obj, int key) {
179  if(key = 4231998) {
180  SDL_Rect src = obj.getFrame();
181  SDL_Rect des = obj.getDest();
182  SDL_RenderCopyEx(engren, obj.getImage().getTexture(), &src, &des, obj.getAngle(), NULL, SDL_FLIP_NONE);
183  }
184 }
185 void Engine::draw(Level lvl) {
186  if(gs.getGameState()!=0) {
187  vector<Tile> tiles = lvl.getTilesToRender();
188  for(int i=0; i<tiles.size(); i++) {
189  draw(tiles[i]);
190  }
191  vector<Object> objects = lvl.getObjectsToRender();
192  for(int i=0; i<objects.size(); i++) {
193  draw(objects[i]);
194  }
195  vector<Entity> entities = lvl.getEntitiesToRender();
196  for(int i=0; i<entities.size(); i++) {
197  draw(entities[i]);
198  }
199  }
200 }
201 
202 void Engine::loadFont(char *font_path) {
203  font = TTF_OpenFont(font_path, 24);
204 }
205 
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;
209 
210  if (font == NULL) {
211  fprintf(stderr, "error: font not found\n");
212  exit(EXIT_FAILURE);
213  }
214  SDL_Color color;
215  color.r = r;
216  color.g = g;
217  color.b = b;
218  color.a = 255;
219  SDL_Rect rect;
220  surface = TTF_RenderText_Solid(font, text, color);
221  texture = SDL_CreateTextureFromSurface(engren, surface);
222  rect.x = x;
223  rect.y = y;
224  rect.w = surface->w;
225  rect.h = surface->h;
226  SDL_FreeSurface(surface);
227  SDL_RenderCopy(engren, texture, NULL, &rect);
228  SDL_DestroyTexture(texture);
229 }
230 
231 void Engine::setBackground(string filename) {
232  Background b;
233  b.setBackground(filename, WIDTH, HEIGHT, engren);
234  background = b;
235  bkg = true;
236 }
238  draw(background);
239 }
240 
242  setColor(0xff, 0x00, 0xff);
243  Object s;
244  s.setImage("../../engine/res/engine-logo.png", getRenderer());
245  //s.setColor(0xFF, 0xFF, 0x00);
246  s.setFrame(0, 0, 256, 256);
247  s.center(WIDTH, HEIGHT);
248  draw(s, 4231998);
249  SDL_RenderPresent(engren);
250  loopStart();
251  SDL_Delay(2300);
252  gs.setGameState(1);
253 }
254 void Engine::bypassSplash(int key) {
255  if(key = 4231998) {
256  gs.setGameState(1);
257  }
258 }
260  if(gs.getGameState() == 0) {
261  return 0;
262  } else {
263  return 1;
264  }
265 }
266 
267 
268 void Engine::debugMode(bool d) {
269  debug = true;
270  gs.setGameState(1);
271  cout << "In debug mode!" << endl;
272  cout << "Which at this time only means bypassing the splashscreen." << endl;
273 }
274 
275 void Engine::hideMouse() {
276  SDL_ShowCursor(false);
277 }
278 
279 void Engine::showMouse() {
280  SDL_ShowCursor(true);
281 }
282 
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;
285 }
286 
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.
Definition: object.h:12
void setName(string s)
Set window name.
Definition: engine.cpp:78
void bypassSplash(int key)
Deactives the splashscreen, requires key.
Definition: engine.cpp:254
void setImage(Image i)
Set the Object&#39;s Image with a given Image.
Definition: object.h:56
void draw(Object obj)
Draw an object on the screen.
Definition: engine.cpp:151
vector< Object > getObjectsToRender()
Return the Objects that are currently on the screen.
Definition: level.cpp:89
SDL_Renderer * getRenderer()
Returns screen renderer.
Definition: engine.cpp:89
void setBackground(Background b)
Set background.
Definition: engine.h:95
void setSize(int w, int y)
Set window size.
Definition: engine.cpp:84
void setFrame(SDL_Rect i)
Set the frame with a given SDL_Rect.
Definition: object.h:72
void splash()
Calls splashscreen at the beginning of the game. This is automatically called unless deactivated...
Definition: engine.cpp:241
vector< Entity > getEntitiesToRender()
Return the Entities that are currently on the screen.
Definition: level.cpp:103
void drawBackground()
Draw background.
Definition: engine.cpp:237
SDL_Texture * getTexture()
Get SDL_Texture.
Definition: image.cpp:9
void loopStart()
Call this at the beginning of a loop to initilaize the loop.
Definition: engine.cpp:99
SDL_Rect getDest() const
Get the destination for the Object to be displayed on screen.
Definition: object.h:68
~Engine()
Decontructs renderer and window and then quits SDL.
Definition: engine.cpp:19
void setPos(int x, int y)
Set window position.
Definition: engine.cpp:81
vector< Tile > getTilesToRender()
Return the Tiles that are currently on the screen.
Definition: level.cpp:73
Object that is a background image that covers the screen.
Definition: background.h:7
double getAngle() const
Get the Object&#39;s angle.
Definition: object.h:60
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.
Definition: background.cpp:8
void update()
Get fps.
Definition: engine.cpp:134
Image getImage() const
Get the Object&#39;s Image.
Definition: object.h:54
void center(int w, int h)
Center the Object based on a width and height.
Definition: object.cpp:11
void debugMode(bool d)
Active debugger with Boolean.
Definition: engine.cpp:268
bool hasSplashed()
Check if the splashscreen has occured.
Definition: engine.cpp:259
This class stores a Stage and Objects and can move them and display them.
Definition: level.h:10
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.
Definition: engine.cpp:26
SDL_Rect getFrame() const
Get the frame that the Object parses from the Image.
Definition: object.h:66
void render()
Call this at the end of the game loop to render.
Definition: engine.cpp:109
void setColor(Uint8 r, Uint8 g, Uint8 b)
Sets SDL color.
Definition: engine.cpp:93