All of the SDL2 graphics stuff.
authorDavid Gow <[email protected]>
Wed, 26 Mar 2014 10:17:00 +0000 (18:17 +0800)
committerDavid Gow <[email protected]>
Wed, 26 Mar 2014 10:17:00 +0000 (18:17 +0800)
(With deprecated, obsolete OpenGL functions! Woo!)

src/Makefile
src/main.cpp
src/screen.cpp [new file with mode: 0644]
src/screen.h [new file with mode: 0644]
src/view.cpp

index 6ab41c8..59e111b 100644 (file)
@@ -1,9 +1,10 @@
 #Makefile
 CXX = g++ -std=gnu++0x -Wall -Werror -Wshadow -pedantic -g
-OBJ = log.o main.o document.o view.o
-LIB = #-lSDL2
+OBJ = log.o main.o document.o view.o screen.o
+LIB = `sdl2-config --libs` -lGL
 OBJPATHS = $(OBJ:%=../obj/%)
 DEPS := $(OBJPATHS:%.o=%.d)
+CFLAGS += `sdl2-config --cflags`
 
 LINKOBJ = $(OBJPATHS)
 
@@ -19,7 +20,7 @@ $(BIN) : $(LINKOBJ)
 
 ../obj/%.o : %.cpp
        @mkdir -p $(dir $@)
-       $(CXX) -c -MMD -o $@ $<
+       $(CXX) $(CFLAGS) -c -MMD -o $@ $<
 
 -include $(DEPS)
 
index 701e163..273d82a 100644 (file)
@@ -2,6 +2,7 @@
 
 #include "document.h"
 #include "view.h"
+#include "screen.h"
 
 using namespace std;
 using namespace IPDF;
@@ -12,7 +13,14 @@ int main(int argc, char ** argv)
        doc.Add(0.5, 0.5, 0.5, 0.5);
 
        View view(doc);
-       view.Render();
+
+       Screen scr;
+
+       while (scr.PumpEvents())
+       {
+               view.Render();
+               scr.Present();
+       }
 
        return 0;
 }
diff --git a/src/screen.cpp b/src/screen.cpp
new file mode 100644 (file)
index 0000000..6909be2
--- /dev/null
@@ -0,0 +1,69 @@
+#include "common.h"
+#include "screen.h"
+
+#include "SDL_opengl.h"
+
+using namespace IPDF;
+using namespace std;
+
+Screen::Screen()
+{
+       SDL_Init(SDL_INIT_VIDEO);
+       m_window = SDL_CreateWindow("IPDF", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
+                       800, 600, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
+
+       if (!m_window)
+       {
+               Fatal("Couldn't create window!");
+       }
+
+       m_gl_context = SDL_GL_CreateContext(m_window);
+
+}
+
+Screen::~Screen()
+{
+       SDL_GL_DeleteContext(m_gl_context);
+       SDL_DestroyWindow(m_window);
+       SDL_Quit();
+}
+
+void Screen::ResizeViewport(int width, int height)
+{
+       glViewport(0, 0, width, height);
+       m_viewport_width = width;
+       m_viewport_height = height;
+}
+
+bool Screen::PumpEvents()
+{
+       SDL_Event evt;
+       bool no_quit_requested = true;
+       while (SDL_PollEvent(&evt))
+       {
+               switch (evt.type)
+               {
+               case SDL_QUIT:
+                       no_quit_requested = false;
+                       break;
+               case SDL_WINDOWEVENT:
+                       switch (evt.window.event)
+                       {
+                       case SDL_WINDOWEVENT_RESIZED:
+                       case SDL_WINDOWEVENT_SIZE_CHANGED:
+                               ResizeViewport(evt.window.data1, evt.window.data2);
+                               break;
+                       }
+                       break;
+               default:
+                       break;
+               }
+       }
+       return no_quit_requested;
+}
+
+void Screen::Present()
+{
+       SDL_GL_SwapWindow(m_window);
+}
+
diff --git a/src/screen.h b/src/screen.h
new file mode 100644 (file)
index 0000000..ebe9da2
--- /dev/null
@@ -0,0 +1,37 @@
+#ifndef _SCREEN_H
+#define _SCREEN_H
+
+#include <SDL.h>
+
+namespace IPDF
+{
+       /*
+        * The "Screen" class handles managing the OS window (using SDL2).
+        */
+       class Screen
+       {
+       public:
+               Screen();
+               ~Screen();
+
+               // 'Pumps' the system event queue.
+               // Returns 'false' if the program should quit.
+               bool PumpEvents();
+
+               // Finishes rendering a frame, and presents it on the screen.
+               void Present();
+
+               // Get the current width/height of the window's viewport.
+               int ViewportWidth() { return m_viewport_width; }
+               int ViewportHeight() { return m_viewport_height; }
+       private:
+               void ResizeViewport(int width, int height);
+
+               int m_viewport_width;
+               int m_viewport_height;
+               SDL_Window *m_window;
+               SDL_GLContext m_gl_context;
+       };
+}
+
+#endif // _SCREEN_H
index fcb841b..40262ac 100644 (file)
@@ -1,14 +1,43 @@
 #include "view.h"
 
+#include "SDL_opengl.h"
+
 using namespace IPDF;
 using namespace std;
 
 void View::Render()
 {
-       Debug("Bounds are %s", m_bounds.Str().c_str());
-       Debug("Objects are:");
+       static bool debug_output_done = false;
+       if (!debug_output_done)
+       {
+               Debug("Bounds are %s", m_bounds.Str().c_str());
+               Debug("Objects are:");
+               for (unsigned id = 0; id < m_document.ObjectCount(); ++id)
+               {
+                       Debug("%u\t%s", id, m_document.m_objects.bounds[id].Str().c_str());
+               }
+               debug_output_done = true;
+       }
+
+       glClearColor(1.f,1.f,1.f,1.f);
+       glClear(GL_COLOR_BUFFER_BIT);
+
+       glMatrixMode(GL_PROJECTION);
+       glLoadIdentity();
+       glOrtho(m_bounds.x, m_bounds.x+m_bounds.w, m_bounds.y + m_bounds.h, m_bounds.y, -1.f, 1.f);
+       glMatrixMode(GL_MODELVIEW);
+       glLoadIdentity();
+
+       glColor4f(0.f,0.f,0.f,1.f);
+       glBegin(GL_QUADS);
        for (unsigned id = 0; id < m_document.ObjectCount(); ++id)
        {
-               Debug("%u\t%s", id, m_document.m_objects.bounds[id].Str().c_str());
+               Rect obj_bounds = m_document.m_objects.bounds[id];
+               glVertex2f(obj_bounds.x, obj_bounds.y);
+               glVertex2f(obj_bounds.x + obj_bounds.w, obj_bounds.y);
+               glVertex2f(obj_bounds.x + obj_bounds.w, obj_bounds.y + obj_bounds.h);
+               glVertex2f(obj_bounds.x, obj_bounds.y + obj_bounds.h);
        }
+       glEnd();
+
 }

UCC git Repository :: git.ucc.asn.au