-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.cpp
141 lines (122 loc) · 3.53 KB
/
engine.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
//Copyright 2010 Sindre Johansen
//
//This file is part of Gravsim.
//
//Gravsim is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//Gravsim is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.
//You should have received a copy of the GNU General Public License
//along with Gravsim. If not, see <http://www.gnu.org/licenses/>.
#include <string>
#include <iostream>
#include "SDL.h"
#include "SDL_image.h"
#include "SDL_ttf.h"
#include "engine.h"
using namespace std;
//World
World::World(int width, int height, int bpp, string caption) {
SDL_Init(SDL_INIT_EVERYTHING);
screen = SDL_SetVideoMode(width, height, bpp, SDL_SWSURFACE);
TTF_Init();
SDL_WM_SetCaption(caption.c_str(), caption.c_str());
this->screenWidth = width;
this->screenHeight = height;
this->bpp = bpp;
fps = 100;
#ifdef skjermsparer
fullscreen = true;
#else
fullscreen = false;
#endif
currentState = NULL;
setVideoMode();
}
void World::changeState(GameState* newState) {
currentState = newState;
newState->setSize(width, height);
}
void World::gameloop() {
SDL_Event* event;
event = new SDL_Event();
quit = false;
timer.restart();
while (quit == false) {
while (SDL_PollEvent(event)) {
this->handle_event(event);
currentState->handle_event(event);
}
int timeDelta = timer.tick(fps);
currentState->logic(timeDelta);
SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format, 0, 0, 0));
currentState->render(screen);
SDL_Flip(screen);
}
}
void World::handle_event(SDL_Event* event) {
if (event->type == SDL_KEYDOWN) {
switch (event->key.keysym.sym){
case SDLK_f:
fullscreen = !fullscreen;
setVideoMode();
break;
case SDLK_ESCAPE:
quit = true;
break;
}
}
if (event->type == SDL_QUIT)
quit = true;
}
void World::setVideoMode() {
if (fullscreen){
this->width = 1024;
this->height = 768;
screen = SDL_SetVideoMode(width, height, bpp, SDL_SWSURFACE ^ SDL_FULLSCREEN);
} else {
this->width = screenWidth;
this->height = screenHeight;
screen = SDL_SetVideoMode(width, height, bpp, SDL_SWSURFACE);
}
if (currentState != NULL) currentState->setSize(width, height);
}
//Bilde
Bilde::Bilde(string filename) {
#ifdef skjermsparer
filename = "C:\\Documents and Settings\\6715\\Mine dokumenter\\src\\c++\\gravsim\\Release\\data\\" + filename;
#else
filename = "data/" + filename;
#endif
surface = SDL_DisplayFormatAlpha(IMG_Load(filename.c_str()));
}
void Bilde::applyTo(SDL_Surface* dest, int x, int y) {
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface(this->surface, NULL, dest, &offset);
}
//GameState
void GameState::setSize(int width, int height) {
this->width = width;
this->height = height;
}
//Timer
Timer::Timer() {
lastTick = SDL_GetTicks();
}
void Timer::restart() {
lastTick = SDL_GetTicks();
}
Uint32 Timer::tick(Uint32 fps){
if ((SDL_GetTicks() - lastTick) < (1000/fps))
SDL_Delay((1000/fps)-(SDL_GetTicks() - lastTick));
Uint32 delta = SDL_GetTicks() - lastTick;
lastTick = SDL_GetTicks();
if (delta > 100) delta = 0; //For å slippe planeter som hopper hit og dit hvis det lagger
return delta;
}