void widgetDraw(widget *self) { int i; // See if we need to be redrawn if (self->needsRedraw) { void *bits = cairo_image_surface_get_data(cairo_get_target(self->cr)); self->needsRedraw = false; // Mark the texture as needing uploading self->textureNeedsUploading = true; // Clear the current context cairo_set_operator(self->cr, CAIRO_OPERATOR_SOURCE); cairo_set_source_rgba(self->cr, 0.0, 0.0, 0.0, 0.0); cairo_paint(self->cr); // Restore the compositing operator back to the default cairo_set_operator(self->cr, CAIRO_OPERATOR_OVER); // Save (push) the current context cairo_save(self->cr); // Redaw ourself widgetDoDraw(self); // Restore the context cairo_restore(self->cr); // Update the texture if widgetEndGL has not done it for us if (self->textureNeedsUploading) { // Flush the cairo surface to ensure that all drawing is completed cairo_surface_flush(cairo_get_target(self->cr)); glBindTexture(GL_TEXTURE_RECTANGLE_ARB, self->textureId); glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA, self->size.x, self->size.y, 0, GL_BGRA, GL_UNSIGNED_BYTE, bits); self->textureNeedsUploading = false; } } // Draw our children (even if we did not need redrawing our children might) for (i = 0; i < vectorSize(self->children); i++) { widget *child = vectorAt(self->children, i); // Ask the child to re-draw itself widgetDraw(child); } }
int main(int argc, char *argv[]) { SDL_Surface *screen; SDL_Event sdlEvent; bool quit = false; lua_State* L; // Make sure everything is cleaned up on quit atexit(SDL_Quit); // Init SDL if (SDL_Init(SDL_INIT_VIDEO) < -1) { fprintf(stderr, "SDL_init: %s\n", SDL_GetError()); exit(1); } // Enable UNICODE support (for key events) SDL_EnableUNICODE(true); // Enable double-buffering SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); // Enable sync-to-vblank SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 1); // 800x600 true colour screen = SDL_SetVideoMode(800, 600, 32, SDL_OPENGL); if (screen == NULL) { fprintf(stderr, "SDL_SetVideoMode: %s\n", SDL_GetError()); exit(1); } SDL_WM_SetCaption("Warzone UI Simulator", NULL); // Init OpenGL glClearColor(0.3f, 0.3f, 0.3f, 0.0f); glCullFace(GL_BACK); glEnable(GL_BLEND); glBlendFunc(GL_CONSTANT_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBlendColor(1.0f, 1.0f, 1.0f, 1.0f); glEnable(GL_TEXTURE_RECTANGLE_ARB); glViewport(0, 0, 800, 600); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0, 800, 600, 0.0, -1.0, 1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); /* * Create the GUI */ widgetSDLInit(); L = lua_open_betawidget(); createGUI(L); // Main event loop while (!quit) { int i; while (SDL_PollEvent(&sdlEvent)) { if (sdlEvent.type == SDL_QUIT) { quit = true; } else { widgetHandleSDLEvent(&sdlEvent); } } // Fire timer events widgetFireTimers(); glClear(GL_COLOR_BUFFER_BIT); for (i = 0; i < vectorSize(windowGetWindowVector()); i++) { window *wnd = vectorAt(windowGetWindowVector(), i); widgetDraw(WIDGET(wnd)); widgetComposite(WIDGET(wnd)); } SDL_GL_SwapBuffers(); } lua_close(L); widgetSDLQuit(); return EXIT_SUCCESS; }