From a156db0aeb0f57f492dfe538129c9f1cb0df2e47 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Wed, 11 Aug 2010 16:18:28 +0000 Subject: [PATCH] The GTK loop now works. Sort of. --- src/editor.cpp | 19 +++++++++++-- src/gtkvideo.cpp | 70 +++++++++++++++++++++++++++++++++++++++++++++--- src/gtkvideo.h | 4 +-- src/sdlvideo.cpp | 4 +-- src/sdlvideo.h | 4 +-- src/test-map.cpp | 6 ++--- src/tileset.cpp | 8 ------ src/video.h | 4 +-- 8 files changed, 95 insertions(+), 24 deletions(-) diff --git a/src/editor.cpp b/src/editor.cpp index ee94d588..ee3842c3 100644 --- a/src/editor.cpp +++ b/src/editor.cpp @@ -51,16 +51,31 @@ int main(int argc, char **argv) GtkVideo *video = new GtkVideo("LOL", 640, 480); glarea = GTK_WIDGET(video->GetWidget()); - /* put glarea into window and show it all */ gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(glarea)); gtk_widget_show(GTK_WIDGET(glarea)); gtk_widget_show(GTK_WIDGET(window)); - while (!quit) +while (g_main_context_iteration(NULL, FALSE)); +if (gtk_gl_area_make_current(GTK_GL_AREA(glarea))) fprintf(stderr, "OK\n"); + Game *game = new Game("maps/testmap.tmx"); + + for (;;) { + // Do GTK stuff until the user wants to quit while (g_main_context_iteration(NULL, FALSE)); + + if (quit) + break; + + video->PreRender(); + game->SetMouse(0, 0); + game->Render(); + video->PostRender(33.33333f); } + delete game; + delete video; + return 0; } diff --git a/src/gtkvideo.cpp b/src/gtkvideo.cpp index 9419d50e..14b12310 100644 --- a/src/gtkvideo.cpp +++ b/src/gtkvideo.cpp @@ -28,6 +28,9 @@ class GtkVideoData private: static gint init(GtkWidget *widget) { + GtkVideoData *data = (GtkVideoData *) + gtk_object_get_data(GTK_OBJECT(widget), "data"); + /* OpenGL functions can be called only if make_current returns true */ if (gtk_gl_area_make_current(GTK_GL_AREA(widget))) { @@ -38,14 +41,38 @@ private: glOrtho(0,100, 100,0, -1,1); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glOrtho(0, data->widget->allocation.width, + data->widget->allocation.height, 0, -1, 10); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + + glEnable(GL_TEXTURE_2D); + glShadeModel(GL_SMOOTH); + glClearColor(0.0f, 0.0f, 0.0f, 0.0f); + glClearDepth(1.0); + glEnable(GL_DEPTH_TEST); + glDepthFunc(GL_LEQUAL); + glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); + + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + } return TRUE; } static gint draw(GtkWidget *widget, GdkEventExpose *event) { - if (event->count == 0 && gtk_gl_area_make_current(GTK_GL_AREA(widget))) + GtkVideoData *data = (GtkVideoData *) + gtk_object_get_data(GTK_OBJECT(widget), "data"); + + if (event->count == 0 && data->drawing == 2 + && gtk_gl_area_make_current(GTK_GL_AREA(widget))) { +#if 0 /* Draw simple triangle */ glClearColor(0,0,0,1); glClear(GL_COLOR_BUFFER_BIT); @@ -55,6 +82,9 @@ private: glVertex2f(10,90); glVertex2f(90,90); glEnd(); +#endif + + data->drawing = 0; /* Swap backbuffer to front */ gtk_gl_area_swapbuffers(GTK_GL_AREA(widget)); @@ -65,15 +95,38 @@ private: static gint reshape(GtkWidget *widget, GdkEventConfigure *event) { + GtkVideoData *data = (GtkVideoData *) + gtk_object_get_data(GTK_OBJECT(widget), "data"); + if (gtk_gl_area_make_current(GTK_GL_AREA(widget))) { glViewport(0,0, widget->allocation.width, widget->allocation.height); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glOrtho(0, data->widget->allocation.width, + data->widget->allocation.height, 0, -1, 10); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + + glEnable(GL_TEXTURE_2D); + glShadeModel(GL_SMOOTH); + glClearColor(0.0f, 0.0f, 0.0f, 0.0f); + glClearDepth(1.0); + glEnable(GL_DEPTH_TEST); + glDepthFunc(GL_LEQUAL); + glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); + + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + } return TRUE; } GtkWidget *widget; + int drawing; }; /* @@ -102,6 +155,9 @@ GtkVideo::GtkVideo(char const *title, int width, int height) } data->widget = gtk_gl_area_new(attrlist); + data->drawing = 0; + + gtk_object_set_data(GTK_OBJECT(data->widget), "data", data); gtk_widget_set_events(GTK_WIDGET(data->widget), GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK); @@ -152,14 +208,22 @@ int GtkVideo::GetHeight() const return data->widget->allocation.height; } -void GtkVideo::Clear() +void GtkVideo::PreRender() { +/// XXX: is this right? +gtk_gl_area_make_current(GTK_GL_AREA(data->widget)); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); + + data->drawing = 1; } -void GtkVideo::Refresh(float milliseconds) +void GtkVideo::PostRender(float milliseconds) { + data->drawing = 2; + + gtk_widget_draw(GTK_WIDGET(data->widget), NULL); #if 0 if (milliseconds > 0.0f) while (SDL_GetTicks() < data->ticks + (milliseconds - 0.5f)) diff --git a/src/gtkvideo.h b/src/gtkvideo.h index ad0b7dc4..c34e0ae4 100644 --- a/src/gtkvideo.h +++ b/src/gtkvideo.h @@ -22,8 +22,8 @@ public: // Inherited virtual int GetWidth() const; virtual int GetHeight() const; - virtual void Clear(); - virtual void Refresh(float milliseconds); + virtual void PreRender(); + virtual void PostRender(float milliseconds); virtual void FullScreen(); private: diff --git a/src/sdlvideo.cpp b/src/sdlvideo.cpp index 95cb6c78..7024489b 100644 --- a/src/sdlvideo.cpp +++ b/src/sdlvideo.cpp @@ -89,13 +89,13 @@ int SdlVideo::GetHeight() const return data->video->h; } -void SdlVideo::Clear() +void SdlVideo::PreRender() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); } -void SdlVideo::Refresh(float milliseconds) +void SdlVideo::PostRender(float milliseconds) { if (milliseconds > 0.0f) while (SDL_GetTicks() < data->ticks + (milliseconds - 0.5f)) diff --git a/src/sdlvideo.h b/src/sdlvideo.h index 08b4c983..00454d87 100644 --- a/src/sdlvideo.h +++ b/src/sdlvideo.h @@ -18,8 +18,8 @@ public: virtual int GetWidth() const; virtual int GetHeight() const; - virtual void Clear(); - virtual void Refresh(float milliseconds); + virtual void PreRender(); + virtual void PostRender(float milliseconds); virtual void FullScreen(); private: diff --git a/src/test-map.cpp b/src/test-map.cpp index 1de962fa..b4e0e7f6 100644 --- a/src/test-map.cpp +++ b/src/test-map.cpp @@ -11,11 +11,11 @@ int main(int argc, char **argv) { Video *video = new SdlVideo("Deus Hax", 640, 480); - Game *game = new Game("maps/testmap-library.tmx"); + Game *game = new Game("maps/testmap.tmx"); for (int done = 0; !done; ) { - video->Clear(); + video->PreRender(); /* Test stuff */ int mx, my; @@ -23,7 +23,7 @@ int main(int argc, char **argv) game->SetMouse(mx * (640 - 32) / 640, my * (480 - 32) / 480); game->Render(); - video->Refresh(33.33333f); + video->PostRender(33.33333f); /* This could go in a separate function */ SDL_Event event; diff --git a/src/tileset.cpp b/src/tileset.cpp index dae4eb5e..fca7c2d3 100644 --- a/src/tileset.cpp +++ b/src/tileset.cpp @@ -26,14 +26,6 @@ class TileSetData friend class TileSet; private: - static int Compare(void const *p1, void const *p2) - { - int const *n1 = (int const *)p1; - int const *n2 = (int const *)p2; - - return n1[2] + 32 * n1[3] - (n2[2] + 32 * n2[3]); - } - char *name; int ref; int *tiles; diff --git a/src/video.h b/src/video.h index efafe83a..7eb4c9e8 100644 --- a/src/video.h +++ b/src/video.h @@ -14,8 +14,8 @@ public: virtual int GetWidth() const = 0; virtual int GetHeight() const = 0; - virtual void Clear() = 0; - virtual void Refresh(float milliseconds) = 0; + virtual void PreRender() = 0; + virtual void PostRender(float milliseconds) = 0; virtual void FullScreen() = 0; };