initial commit
[voxscape] / src / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <math.h>
5 #include <assert.h>
6 #include <GL/glut.h>
7 #include "glfb.h"
8
9 int init(void);
10 void display(void);
11 void idle(void);
12 void reshape(int x, int y);
13 void keyb(unsigned char key, int x, int y);
14
15 int win_width, win_height;
16
17 #define FB_W    640
18 #define FB_H    480
19 unsigned int fb[FB_W * FB_H];
20
21
22 int main(int argc, char **argv)
23 {
24         glutInit(&argc, argv);
25         glutInitWindowSize(1280, 960);
26         glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
27         glutCreateWindow("voxel landscape test");
28
29         glutDisplayFunc(display);
30         glutReshapeFunc(reshape);
31         glutKeyboardFunc(keyb);
32         glutIdleFunc(idle);
33
34         if(init() == -1) {
35                 return 1;
36         }
37
38         glutMainLoop();
39         return 0;
40 }
41
42
43 int init(void)
44 {
45         int i, j, xor, r, g, b;
46         unsigned int *ptr;
47
48         ptr = fb;
49         for(i=0; i<FB_H; i++) {
50                 for(j=0; j<FB_W; j++) {
51                         xor = i ^ j;
52                         r = (xor >> 1) & 0xff;
53                         g = xor & 0xff;
54                         b = (xor << 1) & 0xff;
55                         *ptr++ = b | (g << 8) | (r << 16);
56                 }
57         }
58
59         win_width = glutGet(GLUT_WINDOW_WIDTH);
60         win_height = glutGet(GLUT_WINDOW_HEIGHT);
61
62         glfb_setup(FB_W, FB_H, GLFB_RGBA32, FB_W * 4);
63         return 0;
64 }
65
66 void display(void)
67 {
68         glfb_update(fb);
69         glfb_display();
70
71         glutSwapBuffers();
72         assert(glGetError() == GL_NO_ERROR);
73 }
74
75 void idle(void)
76 {
77         glutPostRedisplay();
78 }
79
80 void reshape(int x, int y)
81 {
82         glViewport(0, 0, x, y);
83
84         win_width = x;
85         win_height = y;
86 }
87
88 void keyb(unsigned char key, int x, int y)
89 {
90         switch(key) {
91         case 27:
92                 exit(0);
93
94         default:
95                 break;
96         }
97 }