-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathemx16demo.cpp
138 lines (108 loc) · 3.3 KB
/
emx16demo.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
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <math.h>
#include "vgalib.h"
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif
#include <iostream>
using namespace std;
typedef struct {
image score;
image playfield;
image ball;
image ballr;
sdl *display;
unsigned int x,y,w,h,dx,dy;
int rot,rotframes;
} context;
void animate(void *arg)
{
context *ctx=(context *)arg;
ctx->display->screen.drawimage(0,0,ctx->score);
ctx->display->screen.drawimage(0,20,ctx->playfield);
ctx->ball.rotate(ctx->ballr,ctx->rot);
ctx->display->screen.drawsprite(ctx->x,ctx->y,ctx->ballr);
ctx->display->update();
ctx->rotframes++;
if(ctx->rotframes%5 == 0)
ctx->rot+=45;
if(ctx->rot>360) ctx->rot=0;
ctx->x+=ctx->dx;
ctx->y+=ctx->dy;
if(ctx->x>ctx->display->width()-ctx->w-1 || ctx->x<1) {
ctx->dx*=-1;
}
if(ctx->y>ctx->display->height()-ctx->h-1 || ctx->y<20) {
ctx->dy*=-1;
}
#ifdef __EMSCRIPTEN__
if (ctx->display->kbhit()) {
printf("Exit...\n");
emscripten_cancel_main_loop();
}
#endif
}
int main(void)
{
context ctx;
ctx.rot=0,ctx.rotframes=0;
ctx.dx=1;
ctx.dy=1;
ctx.x=0;
ctx.y=20;
printf("Starting...\n");
ctx.display=new sdl();
ctx.display->graphmode(adapter::X16);
ctx.display->cls();
ctx.display->setpalette(palette::TEXT_PAL);
canvas::setdefpalette(ctx.display->getpalette());
printf("Loading...\n");
png asset;
printf("Loading assets...\n");
if (!(asset.loadimage("emscripten/assets/score.png",ctx.score) &&
asset.loadimage("emscripten/assets/playfield.png",ctx.playfield) &&
asset.loadimage("emscripten/assets/ball.png",ctx.ball))) {
printf("Error loading sprite\n");
return(1);
}
asset.free();
ctx.w=ctx.ball.width();
ctx.h=ctx.ball.height();
ctx.ballr=ctx.ball;
#ifdef __EMSCRIPTEN__
int simulate_infinite_loop = 1;
emscripten_set_canvas_size(int(ctx.display->getscalewidth()), int(ctx.display->getscaleheight()));
emscripten_set_main_loop_arg(animate, &ctx, -1, simulate_infinite_loop);
#else
uint32_t start_time,end_time,sched_time;
int hz=60; // Target event rate, vsync is what actually controls our speed, this delay is just how long we wait for events, ensuring the render loop always has priority
int clocks_per_frame=1000/hz;
int wait;
bool endprogram=false;
ctx.display->getEvents(20); // debounce
do {
start_time=SDL_GetTicks();
sched_time=start_time+clocks_per_frame;
animate(&ctx);
end_time=SDL_GetTicks();
// wait=(clocks_per_frame-(ntime-stime))/1000; // generate accurate target times in the future
wait=(sched_time - end_time);
wait=wait<0?0:wait; // If render took too long it will result in negative wait.
// cout <<"wait delay "<<wait<<" start "<<start_time<<" sched "<<sched_time<<" end "<<end_time<<" clocks "<<clocks_per_frame<<endl;
if (ctx.display->getEvents(wait) != 0) {
endprogram=true;
}
} while(!endprogram);
#endif
/**
* If simulate_infinite_loop = 0, emscripten_set_main_loop_arg won't block
* and this code will run straight away.
* If simulate_infinite_loop = 1 then this code will not be reached
*/
printf("quitting...\n");
delete ctx.display;
return 0;
}