-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfoo.c
185 lines (147 loc) · 3.72 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include "SDL.h"
#include <cairo.h>
#include <math.h>
int fps = 30;
int w = 320;
int h = 240;
float scale = 1.0;
typedef float vec[4];
typedef float mat[4 * 4];
typedef float *mat_p;
void mat_identity(mat m) {
int i;
for (i = 0; i < 16; i = i + 1) {
m[i] = 0;
}
m[ 0] = 1; m[ 5] = 1; m[10] = 1; m[15] = 1;
}
void mat_load(mat m, mat n) {
int i;
for (i = 0; i < 16; i = i + 1) {
m[i] = n[i];
}
}
void mat_mul(mat m, mat n) {
mat o;
int i, j;
for (j = 0; j < 4; j = j + 1) {
for (i = 0; i < 4; i = i + 1) {
o[4*j+i] = m[4*j+0] * n[4*0+i] + m[4*j+1] * n[4*1+i]
+ m[4*j+2] * n[4*2+i] + m[4*j+3] * n[4*3+i];
}
}
for (i = 0; i < 16; i = i + 1) {
m[i] = o[i];
}
}
void vec_mat_mul(vec v, mat m) {
float x, y, z, w;
x = v[0] * m[ 0] + v[1] * m[ 1] + v[2] * m[ 2] + v[3] * m[ 3];
y = v[0] * m[ 4] + v[1] * m[ 5] + v[2] * m[ 6] + v[3] * m[ 7];
z = v[0] * m[ 8] + v[1] * m[ 9] + v[2] * m[10] + v[3] * m[11];
w = v[0] * m[12] + v[1] * m[13] + v[2] * m[14] + v[3] * m[15];
v[0] = x; v[1] = y; v[2] = z; v[3] = w;
}
int main(int argc, char **argv) {
SDL_Surface *sdl_surface;
cairo_t *cr;
Uint8 *keystate;
Uint32 next_frame;
float aspect = 1.0 * w/h;
mat matrix;
int running;
float angle;
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
SDL_ShowCursor(0);
SDL_SetVideoMode(w, h, 32, 0);
sdl_surface = SDL_GetVideoSurface();
{ /* Initialize 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);
cairo_surface_destroy(surface);
}
// Cartesian
cairo_translate(cr, w/2.0, h/2.0);
cairo_scale(cr, 1, -1);
// fixed scale
cairo_scale(cr, h/scale, h/scale);
/* Initialize Delay */
next_frame = 1024.0 / fps;
/* Game Logic */
running = 1;
{
mat_p m = matrix;
float a = M_PI / 4.0;
mat_identity(m);
m[11] = 8;
m[ 5] = cos(a); m[ 6] = -sin(a);
m[ 9] = sin(a); m[10] = cos(a);
}
angle = 0;
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);
{
mat m, n; vec v;
float x, y, a = angle;
mat_identity(n);
n[0] = cos(a); n[1] = -sin(a);
n[4] = sin(a); n[5] = cos(a);
mat_load(m, matrix);
mat_mul(m, n);
v[0] = -1; v[1] = -1; v[2] = 0; v[3] = 1;
vec_mat_mul(v, m);
x = v[0] / v[2]; y = v[1] / v[2];
cairo_move_to(cr, x, y);
v[0] = 1; v[1] = -1; v[2] = 0; v[3] = 1;
vec_mat_mul(v, m);
x = v[0] / v[2]; y = v[1] / v[2];
cairo_line_to(cr, x, y);
v[0] = 1; v[1] = 1; v[2] = 0; v[3] = 1;
vec_mat_mul(v, m);
x = v[0] / v[2]; y = v[1] / v[2];
cairo_line_to(cr, x, y);
v[0] = -1; v[1] = 1; v[2] = 0; v[3] = 1;
vec_mat_mul(v, m);
x = v[0] / v[2]; y = v[1] / v[2];
cairo_line_to(cr, x, y);
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 */
SDL_PumpEvents();
keystate = SDL_GetKeyState(NULL);
if (keystate[SDLK_q]) {
running = 0;
}
angle = angle + M_PI / 2.0 / fps;
}
SDL_UnlockSurface(sdl_surface);
cairo_destroy(cr);
SDL_Quit();
return 0;
}