dostunnel

view src/tunnel.c @ 3:a8024271c662

added timer
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 16 Mar 2013 03:35:08 +0200
parents 5d7f784002b0
children ba868d348de8
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <math.h>
5 #include <conio.h>
6 #include "wvga.h"
7 #include "texture.h"
8 #include "palman.h"
9 #include "timer.h"
11 #ifndef M_PI
12 #define M_PI 3.14159265
13 #endif
15 #define WIDTH 320
16 #define HEIGHT 200
18 int init(void);
19 void cleanup(void);
20 void display(void);
21 int calc_tunnel_mapping(void);
22 int parse_args(int argc, char **argv);
24 static unsigned char *fbuf;
25 static struct texture *tex;
26 static int colrange;
28 static char *tex_fname;
30 static unsigned long *umap, *vmap;
32 static unsigned long frames;
33 static unsigned long start_time;
35 static int under_windows;
38 int main(int argc, char **argv)
39 {
40 if(parse_args(argc, argv) == -1) {
41 return 1;
42 }
44 if(init() == -1) {
45 return 1;
46 }
48 while(!kbhit()) {
49 display();
50 }
51 cleanup();
52 return 0;
53 }
55 int init(void)
56 {
57 int i;
58 struct palm_color *pal;
60 if(!(fbuf = malloc(WIDTH * HEIGHT))) {
61 fprintf(stderr, "failed to allocate framebuffer\n");
62 return -1;
63 }
65 if(calc_tunnel_mapping() == -1) {
66 fprintf(stderr, "failed to precalc tunnel mapping\n");
67 return 1;
68 }
70 if(tex_fname) {
71 if(!(tex = load_texture(tex_fname))) {
72 fprintf(stderr, "failed to load texture: %s\n", tex_fname);
73 free(fbuf);
74 return -1;
75 }
76 } else {
77 fprintf(stderr, "you must specify a texture to use\n");
78 return -1;
79 }
81 set_video_mode(0x13);
83 palm_build();
84 get_texture_pixels(tex);
86 pal = palm_palette();
87 for(i=0; i<palm_palette_size(); i++) {
88 set_pal_entry(i, pal[i].r, pal[i].g, pal[i].b);
89 }
90 colrange = palm_color_range();
92 init_timer(120);
93 start_time = get_msec();
95 return 0;
96 }
98 void cleanup(void)
99 {
100 unsigned long sec = (get_msec() - start_time) / 1000ul;
102 free_texture(tex);
103 free(fbuf);
104 set_video_mode(0x3);
106 if(sec) {
107 printf("avg fps: %lu\n", frames / sec);
108 }
109 }
111 void display(void)
112 {
113 unsigned long msec = get_msec() - start_time;
115 unsigned int i;
116 unsigned char voffs = msec >> 3;
117 unsigned char uoffs = msec >> 6;
119 unsigned char *fbptr = fbuf;
120 unsigned long *uptr = umap;
121 unsigned long *vptr = vmap;
123 for(i=0; i<64000; i++) {
124 unsigned long u = *uptr++;
125 unsigned long v = *vptr++;
126 unsigned long tx = ((((unsigned long)(u - uoffs) << 3) & 0xff) * tex->width) >> 8;
127 unsigned long ty = (((unsigned long)(v + voffs) & 0xff) * tex->height) >> 8;
129 unsigned long base = tex->pixels[ty * tex->width + tx];
130 long zcue_shift = colrange - (v >> 6);
131 if(zcue_shift < 0) {
132 zcue_shift = 0;
133 }
134 *fbptr++ = (unsigned char)(base + zcue_shift);
135 }
137 copy_frame(fbuf);
138 frames++;
139 }
141 int calc_tunnel_mapping(void)
142 {
143 int i, j;
144 unsigned long *uptr, *vptr;
145 float diag_dist = sqrt(1.33333 * 1.33333 + 1.0);
147 if(!(umap = malloc(WIDTH * HEIGHT * sizeof *umap))) {
148 return -1;
149 }
150 if(!(vmap = malloc(WIDTH * HEIGHT * sizeof *umap))) {
151 free(umap);
152 return -1;
153 }
155 uptr = umap;
156 vptr = vmap;
158 for(i=0; i<HEIGHT; i++) {
159 for(j=0; j<WIDTH; j++) {
160 float x = ((float)j / WIDTH * 2.0 - 1.0) * 1.33333;
161 float y = (float)i / HEIGHT * 2.0 - 1.0;
162 float angle, z, dist;
164 if(fabs(x) > 0.00001) {
165 angle = atan2(y, x) + M_PI;
166 } else {
167 angle = y < 0.0 ? M_PI / 2.0 : 3.0 * M_PI / 2.0;
168 }
169 dist = sqrt(x * x + y * y);
170 z = 2.0 / dist;
172 *uptr++ = (unsigned int)(angle * 0.5 / M_PI * 255.0);
173 *vptr++ = (unsigned int)(z * 255.0);
174 }
175 }
176 return 0;
177 }
179 int parse_args(int argc, char **argv)
180 {
181 int i;
183 for(i=1; i<argc; i++) {
184 if(argv[i][0] == '-') {
185 if(strcmp(argv[i], "-win") == 0) {
186 under_windows = 1;
187 } else {
188 fprintf(stderr, "invalid option: %s\n", argv[i]);
189 return -1;
190 }
191 } else {
192 if(tex_fname) {
193 fprintf(stderr, "unexpected argument: %s\n", argv[i]);
194 return -1;
195 }
196 tex_fname = argv[i];
197 }
198 }
200 if(!tex_fname) {
201 tex_fname = "data/wall2.ppm";
202 }
203 return 0;
204 }