#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)
../obj/%.o : %.cpp
@mkdir -p $(dir $@)
- $(CXX) -c -MMD -o $@ $<
+ $(CXX) $(CFLAGS) -c -MMD -o $@ $<
-include $(DEPS)
#include "document.h"
#include "view.h"
+#include "screen.h"
using namespace std;
using namespace IPDF;
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;
}
--- /dev/null
+#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);
+}
+
--- /dev/null
+#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
#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();
+
}