foo
[erebus2020] / xerebus / src / main.c
1 #include <stdio.h>
2 #include "miniglut.h"
3 #include "erebus.h"
4
5 static void display(void);
6 static void reshape(int x, int y);
7 static void keyb(unsigned char key, int x, int y);
8 static void mouse(int bn, int st, int x, int y);
9 static void motion(int x, int y);
10
11 static int win_width, win_height;
12 static float win_aspect;
13
14 static struct erb_rend *erb;
15
16 static int mouse_x, mouse_y;
17 static int drag_x, drag_y;
18 static int drag;
19
20
21 int main(int argc, char **argv)
22 {
23         glutInit(&argc, argv);
24         glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
25         glutInitWindowSize(1280, 800);
26         glutCreateWindow("X erebus");
27
28         glutDisplayFunc(display);
29         glutReshapeFunc(reshape);
30         glutKeyboardFunc(keyb);
31         glutMouseFunc(mouse);
32         glutMotionFunc(motion);
33
34         if(!(erb = erb_create())) {
35                 return 1;
36         }
37         reshape_pending = 1;
38
39         glutMainLoop();
40         erb_destroy(erb);
41         return 0;
42 }
43
44 static void display(void)
45 {
46         if(reshape_pending) {
47                 reshape_pending = 0;
48
49                 erb_allocframe(erb, x, y);
50                 erb_begin(erb);
51
52                 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, win_width, win_height, 0,
53                                 GL_RGB, GL_FLOAT, erb_getframe(erb));
54         }
55         /* TODO: continue with getting notified when a block is done rendering */
56
57         glClear(GL_COLOR_BUFFER_BIT);
58
59         if(drag) {
60                 glEnable(GL_LOGIC_OP);
61                 glLogicOp(GL_XOR);
62
63                 glBegin(GL_LINE_LOOP);
64                 glColor3f(1, 1, 1);
65                 glVertex2f(drag_x, drag_y);
66                 glVertex2f(mouse_x, drag_y);
67                 glVertex2f(mouse_x, mouse_y);
68                 glVertex2f(drag_x, mouse_y);
69                 glEnd();
70
71                 glDisable(GL_LOGIC_OP);
72         }
73
74         glutSwapBuffers();
75 }
76
77 static void reshape(int x, int y)
78 {
79         win_width = x;
80         win_height = y;
81         win_aspect = (float)x / (float)y;
82
83         glViewport(0, 0, x, y);
84
85         glMatrixMode(GL_PROJECTION);
86         glLoadIdentity();
87         glOrtho(0, x, y, 0, -1, 1);
88
89         reshape_pending = 1;
90         glutPostRedisplay();
91 }
92
93 static void keyb(unsigned char key, int x, int y)
94 {
95         if(key == 27) {
96                 glutExit();
97         }
98 }
99
100 static void mouse(int bn, int st, int x, int y)
101 {
102         int rect[4];
103
104         mouse_x = x;
105         mouse_y = y;
106
107         if(bn == 0) {
108                 if(st == GLUT_DOWN) {
109                         drag = 1;
110                         drag_x = x;
111                         drag_y = y;
112                 } else {
113                         drag = 0;
114
115                         rect[0] = x < drag_x ? x : drag_x;
116                         rect[1] = y < drag_y ? y : drag_y;
117                         rect[2] = abs(x - drag_x);
118                         rect[3] = abs(y - drag_y);
119                         printf("rect: %d,%d %dx%d\n", rect[0], rect[1], rect[2], rect[3]);
120                 }
121
122                 glutPostRedisplay();
123         }
124 }
125
126 static void motion(int x, int y)
127 {
128         mouse_x = x;
129         mouse_y = y;
130
131         glutPostRedisplay();
132 }