-
Notifications
You must be signed in to change notification settings - Fork 0
/
foo.c
142 lines (114 loc) · 2.87 KB
/
foo.c
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
142
#include "SDL.h"
#include <cairo.h>
#include <stdlib.h>
int fps = 30;
int width = 320;
int height = 240;
float aspect = 320/240.0;
int rps = 4;
int main(int argc, char **argv) {
SDL_Surface *sdl_surface;
cairo_t *cr;
Uint32 next_frame, next_rand;
int running;
float drop[16], drop_vel[16];
/* Initialize SDL */
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
SDL_ShowCursor(0);
SDL_SetVideoMode(width, height, 32, 0);
sdl_surface = SDL_GetVideoSurface();
{ /* Initialize Cairo Canvas */
cairo_surface_t *surface;
surface = cairo_image_surface_create_for_data(
sdl_surface->pixels,
CAIRO_FORMAT_RGB24,
sdl_surface->w,
sdl_surface->h,
sdl_surface->pitch
);
cr = cairo_create(surface);
// Reduce surface refcount.
// Surface will be freed when canvas is destroyed.
cairo_surface_destroy(surface);
}
// Cartesian
cairo_translate(cr, width/2.0, height/2.0);
cairo_scale(cr, 1, -1);
// scale -- 4:3 :: 16:12
cairo_scale(cr, height / 12, height / 12);
/* Initialize Delay */
next_frame = 1024.0 / fps;
/* Game Logic */
running = 1;
{
int i;
for (i = 0; i < 16; i = i + 1) {
drop[i] = -6 - 3;
drop_vel[i] = -rand() % 3 - 1;
}
next_rand = 1024.0 / rps;
}
SDL_LockSurface(sdl_surface);
while (running) {
/* Render Frame */
cairo_set_operator(cr, CAIRO_OPERATOR_CLEAR);
cairo_paint(cr);
cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
{
int i;
for (i = 0; i < 16; i = i + 1) {
cairo_move_to(cr, i - 8, drop[i] + 3);
cairo_line_to(cr, i - 7, drop[i] + 3);
cairo_line_to(cr, i - 7, drop[i]);
cairo_line_to(cr, i - 8, drop[i]);
cairo_close_path(cr);
}
cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
cairo_fill(cr);
}
/* Update Display */
SDL_UnlockSurface(sdl_surface);
SDL_Flip(sdl_surface);
SDL_LockSurface(sdl_surface);
{ /* Delay */
Uint32 now;
now = SDL_GetTicks();
if (now < next_frame) {
SDL_Delay(next_frame - now);
}
next_frame = next_frame + 1024.0 / fps;
}
{ /* Game Logic */
Uint8 *keystate;
int i;
Uint32 now;
SDL_PumpEvents();
keystate = SDL_GetKeyState(NULL);
if (keystate[SDLK_q]) {
running = 0;
}
for (i = 0; i < 16; i = i + 1) {
if (drop[i] > -6 - 3) {
drop[i] = drop[i] + drop_vel[i] / fps;
}
}
now = SDL_GetTicks();
if (now > next_rand) {
for (i = 0; i < 16; i = i + 1) {
if (drop[i] > -6 - 3) {
drop_vel[i] = -rand() % 3 - 1;
} else {
if (rand() % 32 == 0) {
drop[i] = 6;
}
}
}
next_rand = next_rand + 1024.0 / rps;
}
}
}
SDL_UnlockSurface(sdl_surface);
cairo_destroy(cr);
SDL_Quit();
return 0;
}