-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrspace.cc
80 lines (64 loc) · 1.61 KB
/
scrspace.cc
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
#include <iostream>
#include <exception>
#include <cmath>
#include <ctime>
#include <SDL2pp/SDL2pp.hh>
#define TICKRATE_MS 500
using namespace SDL2pp;
long long clock_ms() {
struct timespec t;
clock_gettime(CLOCK_MONOTONIC, &t);
return t.tv_sec * 1000LL + lround(t.tv_nsec / 1e6);
}
class System {
std::array<int, 2> viewOrigin;
std::array<int, 2> viewSize;
SDL sdl;
Window win;
Renderer ren;
Texture* stageBack;
void scroll();
public:
System();
void render();
void checkInput();
void checkCollisions();
bool isDone();
void changeStage(std::string name);
};
System::System() : sdl(SDL_INIT_VIDEO), win("scrspace", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
480, 640, SDL_WINDOW_SHOWN), ren(win, -1, SDL_RENDERER_ACCELERATED) {
ren.SetDrawBlendMode(SDL_BLENDMODE_BLEND);
viewOrigin = {15, 20};
viewSize = {15, 20};
}
void System::changeStage(std::string name) {
stageBack = new Texture(ren, "res/" + name + ".png");
}
void System::render() {
ren.Clear();
ren.Copy(*stageBack, NullOpt, Point(0, 0));
ren.Present();
}
int main() try {
long long curtime, prevtime, deltatime, accumulator;
prevtime = clock_ms();
curtime = deltatime = accumulator = 0;
bool done = false;
System sys;
sys.changeStage("stage01");
while (!done) {
while (accumulator >= TICKRATE_MS) {
accumulator -= TICKRATE_MS;
}
//render input sound etc
sys.render();
deltatime = (curtime = clock_ms()) - prevtime;
accumulator += deltatime;
prevtime = curtime;
}
return 0;
} catch (std::exception& e) {
std::cerr << e.what() << std::endl;
return -1;
}