foo
[erebus2020] / xerebus / src / main.c
index 3fed7e6..59a03da 100644 (file)
@@ -1,6 +1,132 @@
 #include <stdio.h>
+#include "miniglut.h"
+#include "erebus.h"
+
+static void display(void);
+static void reshape(int x, int y);
+static void keyb(unsigned char key, int x, int y);
+static void mouse(int bn, int st, int x, int y);
+static void motion(int x, int y);
+
+static int win_width, win_height;
+static float win_aspect;
+
+static struct erb_rend *erb;
+
+static int mouse_x, mouse_y;
+static int drag_x, drag_y;
+static int drag;
+
 
 int main(int argc, char **argv)
 {
+       glutInit(&argc, argv);
+       glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
+       glutInitWindowSize(1280, 800);
+       glutCreateWindow("X erebus");
+
+       glutDisplayFunc(display);
+       glutReshapeFunc(reshape);
+       glutKeyboardFunc(keyb);
+       glutMouseFunc(mouse);
+       glutMotionFunc(motion);
+
+       if(!(erb = erb_create())) {
+               return 1;
+       }
+       reshape_pending = 1;
+
+       glutMainLoop();
+       erb_destroy(erb);
        return 0;
 }
+
+static void display(void)
+{
+       if(reshape_pending) {
+               reshape_pending = 0;
+
+               erb_allocframe(erb, x, y);
+               erb_begin(erb);
+
+               glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, win_width, win_height, 0,
+                               GL_RGB, GL_FLOAT, erb_getframe(erb));
+       }
+       /* TODO: continue with getting notified when a block is done rendering */
+
+       glClear(GL_COLOR_BUFFER_BIT);
+
+       if(drag) {
+               glEnable(GL_LOGIC_OP);
+               glLogicOp(GL_XOR);
+
+               glBegin(GL_LINE_LOOP);
+               glColor3f(1, 1, 1);
+               glVertex2f(drag_x, drag_y);
+               glVertex2f(mouse_x, drag_y);
+               glVertex2f(mouse_x, mouse_y);
+               glVertex2f(drag_x, mouse_y);
+               glEnd();
+
+               glDisable(GL_LOGIC_OP);
+       }
+
+       glutSwapBuffers();
+}
+
+static void reshape(int x, int y)
+{
+       win_width = x;
+       win_height = y;
+       win_aspect = (float)x / (float)y;
+
+       glViewport(0, 0, x, y);
+
+       glMatrixMode(GL_PROJECTION);
+       glLoadIdentity();
+       glOrtho(0, x, y, 0, -1, 1);
+
+       reshape_pending = 1;
+       glutPostRedisplay();
+}
+
+static void keyb(unsigned char key, int x, int y)
+{
+       if(key == 27) {
+               glutExit();
+       }
+}
+
+static void mouse(int bn, int st, int x, int y)
+{
+       int rect[4];
+
+       mouse_x = x;
+       mouse_y = y;
+
+       if(bn == 0) {
+               if(st == GLUT_DOWN) {
+                       drag = 1;
+                       drag_x = x;
+                       drag_y = y;
+               } else {
+                       drag = 0;
+
+                       rect[0] = x < drag_x ? x : drag_x;
+                       rect[1] = y < drag_y ? y : drag_y;
+                       rect[2] = abs(x - drag_x);
+                       rect[3] = abs(y - drag_y);
+                       printf("rect: %d,%d %dx%d\n", rect[0], rect[1], rect[2], rect[3]);
+               }
+
+               glutPostRedisplay();
+       }
+}
+
+static void motion(int x, int y)
+{
+       mouse_x = x;
+       mouse_y = y;
+
+       glutPostRedisplay();
+}