Преглед изворни кода

The GTK loop now works. Sort of.

legacy
Sam Hocevar sam пре 14 година
родитељ
комит
a156db0aeb
8 измењених фајлова са 95 додато и 24 уклоњено
  1. +17
    -2
      src/editor.cpp
  2. +67
    -3
      src/gtkvideo.cpp
  3. +2
    -2
      src/gtkvideo.h
  4. +2
    -2
      src/sdlvideo.cpp
  5. +2
    -2
      src/sdlvideo.h
  6. +3
    -3
      src/test-map.cpp
  7. +0
    -8
      src/tileset.cpp
  8. +2
    -2
      src/video.h

+ 17
- 2
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;
}

+ 67
- 3
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))


+ 2
- 2
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:


+ 2
- 2
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))


+ 2
- 2
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:


+ 3
- 3
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;


+ 0
- 8
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;


+ 2
- 2
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;
};



Loading…
Откажи
Сачувај