void cleanup_mem(void) { int i; destroy_url_list(); history_destroy(); command_cleanup(); queue_destroy(buddy_request_queue); cleanup_manufacture(); cleanup_text_buffers(); cleanup_fonts(); destroy_all_actors(); end_actors_lists(); cleanup_lights(); /* 2d objects */ destroy_all_2d_objects(); destroy_all_2d_object_defs(); /* 3d objects */ destroy_all_3d_objects(); /* caches */ cache_e3d->free_item = &destroy_e3d; cache_delete(cache_e3d); cache_e3d = NULL; #ifdef NEW_TEXTURES free_texture_cache(); #endif // This should be fixed now Sir_Odie cache_delete(cache_system); cache_system = NULL; /* map location information */ for (i = 0; continent_maps[i].name; i++) { free(continent_maps[i].name); } free (continent_maps); destroy_hash_table(server_marks); for (i = 0; i < video_modes_count; i++) { if (video_modes[i].name) free(video_modes[i].name); } free_shaders(); }
int main(int argc, char* argv[]) { GPU_Target* screen; printRenderers(); screen = GPU_Init(800, 600, GPU_DEFAULT_INIT_FLAGS); if(screen == NULL) return -1; printCurrentRenderer(); { Uint32 startTime; long frameCount; Uint8 done; SDL_Event event; GPU_Image* image; Uint32 v, f, p; GPU_ShaderBlock block; int uloc; int timeloc; float dt; #define MAX_SPRITES 50 int numSprites; float x[MAX_SPRITES]; float y[MAX_SPRITES]; float velx[MAX_SPRITES]; float vely[MAX_SPRITES]; int i; image = GPU_LoadImage("data/test.bmp"); if(image == NULL) return -1; block = load_shaders(&v, &f, &p); uloc = GPU_GetUniformLocation(p, "tex"); GPU_SetUniformi(uloc, 0); timeloc = GPU_GetUniformLocation(p, "time"); dt = 0.010f; startTime = SDL_GetTicks(); frameCount = 0; numSprites = 1; for(i = 0; i < MAX_SPRITES; i++) { x[i] = rand()%screen->w; y[i] = rand()%screen->h; velx[i] = 10 + rand()%screen->w/10; vely[i] = 10 + rand()%screen->h/10; } done = 0; while(!done) { while(SDL_PollEvent(&event)) { if(event.type == SDL_QUIT) done = 1; else if(event.type == SDL_KEYDOWN) { if(event.key.keysym.sym == SDLK_ESCAPE) done = 1; else if(event.key.keysym.sym == SDLK_EQUALS || event.key.keysym.sym == SDLK_PLUS) { if(numSprites < MAX_SPRITES) numSprites++; } else if(event.key.keysym.sym == SDLK_MINUS) { if(numSprites > 0) numSprites--; } else if(event.key.keysym.sym == SDLK_SPACE) { if(GPU_IsDefaultShaderProgram(GPU_GetCurrentShaderProgram())) { GPU_ActivateShaderProgram(p, &block); uloc = GPU_GetUniformLocation(p, "tex"); GPU_SetUniformi(uloc, 0); timeloc = GPU_GetUniformLocation(p, "time"); } else GPU_ActivateShaderProgram(0, NULL); } } } for(i = 0; i < numSprites; i++) { x[i] += velx[i]*dt; y[i] += vely[i]*dt; if(x[i] < 0) { x[i] = 0; velx[i] = -velx[i]; } else if(x[i]> screen->w) { x[i] = screen->w; velx[i] = -velx[i]; } if(y[i] < 0) { y[i] = 0; vely[i] = -vely[i]; } else if(y[i]> screen->h) { y[i] = screen->h; vely[i] = -vely[i]; } } GPU_SetUniformf(timeloc, SDL_GetTicks()/1000.0f); GPU_Clear(screen); for(i = 0; i < numSprites; i++) { GPU_Blit(image, NULL, screen, x[i], y[i]); } GPU_Flip(screen); frameCount++; if(frameCount%500 == 0) printf("Average FPS: %.2f\n", 1000.0f*frameCount/(SDL_GetTicks() - startTime)); } printf("Average FPS: %.2f\n", 1000.0f*frameCount/(SDL_GetTicks() - startTime)); GPU_FreeImage(image); free_shaders(v, f, p); } GPU_Quit(); return 0; }
/** * edges using geometry shader. */ int build_triangle_edges(lulog *log, GLuint *program) { int status = LU_OK; luary_uint32 *shaders = NULL; try(compile_shader_from_file(log, GL_VERTEX_SHADER, "flat_model_g.vert", &shaders)); try(compile_shader_from_file(log, GL_GEOMETRY_SHADER, "edge_lines.geom", &shaders)); try(compile_shader_from_file(log, GL_FRAGMENT_SHADER, "direct_colour.frag", &shaders)); try(link_program(log, shaders, program)); finally: status = free_shaders(log, &shaders, status); return status; } /** * copy a frame by rendering a texture directly (needs a quad to select the area). */ int build_direct_texture(lulog *log, direct_texture *program) { int status = LU_OK; luary_uint32 *shaders = NULL; try(compile_shader_from_file(log, GL_VERTEX_SHADER, "direct_texture.vert", &shaders)); try(compile_shader_from_file(log, GL_FRAGMENT_SHADER, "direct_texture.frag", &shaders)); try(link_program(log, shaders, &program->name)); try(set_uniform(log, program->name, "frame", &program->frame, 0)); finally: status = free_shaders(log, &shaders, status); return status; } /** * merge two frames via textures. */ int build_merge_frames(lulog *log, merge_frames *program) { int status = LU_OK; luary_uint32 *shaders = NULL; try(compile_shader_from_file(log, GL_VERTEX_SHADER, "direct_texture.vert", &shaders)); try(compile_shader_from_file(log, GL_FRAGMENT_SHADER, "merge_frames.frag", &shaders)); try(link_program(log, shaders, &program->name)); try(set_uniform(log, program->name, "frame1", &program->frame1, 0)); try(set_uniform(log, program->name, "frame2", &program->frame2, 1)); finally: status = free_shaders(log, &shaders, status); return status; } /** * blur a frame (roughly uniform over 5 pixels radius). */ int build_blur(lulog *log, blur *program) { int status = LU_OK; luary_uint32 *shaders = NULL; try(compile_shader_from_file(log, GL_VERTEX_SHADER, "direct_texture.vert", &shaders)); try(compile_shader_from_file(log, GL_FRAGMENT_SHADER, "blur.frag", &shaders)); try(link_program(log, shaders, &program->name)); try(set_uniform(log, program->name, "frame", &program->frame, 0)); try(set_uniform(log, program->name, "horizontal", &program->horizontal, 1)); finally: status = free_shaders(log, &shaders, status); return status; } int draw_triangle_edges(lulog *log, model *model, programs *programs) { int status = LU_OK; gl_try(glBindVertexArray(model->vao)); // gl_try(glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)); gl_try(glUseProgram(programs->triangle_edges)); gl_try(glMultiDrawArrays(GL_TRIANGLE_STRIP, model->offsets->i, model->counts->i, model->counts->mem.used)); // gl_try(glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)); finally: GL_CLEAN(glBindVertexArray(0)) GL_CLEAN(glUseProgram(0)) return status; }
int main(int argc, char* argv[]) { GPU_Target* screen; printRenderers(); screen = GPU_Init(800, 600, GPU_DEFAULT_INIT_FLAGS); if(screen == NULL) return -1; printCurrentRenderer(); { Uint32 startTime; long frameCount; Uint8 done; SDL_Event event; GPU_Image* image; #define MAX_SPRITES 100 int numSprites; float positions[2*MAX_SPRITES]; float colors[4*4*MAX_SPRITES]; float expanded_colors[4*MAX_SPRITES]; float src_rects[4*MAX_SPRITES]; Uint32 v, f, p; GPU_ShaderBlock block; Uint8 shader_index; int i; SDL_Color color = {255, 255, 255, 255}; SDL_Color red = {255, 0, 0, 255}; SDL_Color green = {0, 255, 0, 255}; SDL_Color blue = {0, 0, 255, 255}; GPU_Rect src_rect; int mx, my; Uint32 mouse_state; image = GPU_LoadImage("data/happy_50x50.bmp"); if(image == NULL) return -1; numSprites = 0; color_attr.format = GPU_MakeAttributeFormat(4, GPU_TYPE_FLOAT, 0, 4*sizeof(float), 0); color_attr.format.is_per_sprite = 0; color_attr.values = colors; block = load_shaders(&v, &f, &p); shader_index = 1; set_shader(p, &block); startTime = SDL_GetTicks(); frameCount = 0; src_rect.x = 0; src_rect.y = 0; src_rect.w = image->w; src_rect.h = image->h; add_sprite(positions, colors, expanded_colors, src_rects, &numSprites, color, src_rect); done = 0; while(!done) { while(SDL_PollEvent(&event)) { if(event.type == SDL_QUIT) done = 1; else if(event.type == SDL_MOUSEBUTTONDOWN) { if(event.button.x <= 150 && event.button.y <= 150) { if(event.button.button == SDL_BUTTON_LEFT) { float dx = event.button.x/3 - src_rect.x; float dy = event.button.y/3 - src_rect.y; src_rect.x = event.button.x/3; src_rect.y = event.button.y/3; src_rect.w -= dx; src_rect.h -= dy; } else if(event.button.button == SDL_BUTTON_RIGHT) { src_rect.w = event.button.x/3 - src_rect.x; src_rect.h = event.button.y/3 - src_rect.y; } } } else if(event.type == SDL_KEYDOWN) { if(event.key.keysym.sym == SDLK_ESCAPE) done = 1; else if(event.key.keysym.sym == SDLK_EQUALS || event.key.keysym.sym == SDLK_PLUS) { if(numSprites < MAX_SPRITES) add_sprite(positions, colors, expanded_colors, src_rects, &numSprites, color, src_rect); } else if(event.key.keysym.sym == SDLK_MINUS) { if(numSprites > 0) numSprites--; } else if(event.key.keysym.sym == SDLK_SPACE) { shader_index++; shader_index %= 2; if(shader_index == 0) set_shader(0, NULL); else if(shader_index == 1) set_shader(p, &block); } else if(event.key.keysym.sym == SDLK_RETURN) { use_color_expansion = !use_color_expansion; if(use_color_expansion) { GPU_LogError("Using attribute expansion.\n"); color_attr.format.is_per_sprite = 1; color_attr.values = expanded_colors; } else { GPU_LogError("Using per-vertex attributes.\n"); color_attr.format.is_per_sprite = 0; color_attr.values = colors; } } } } mouse_state = SDL_GetMouseState(&mx, &my); if(mouse_state & (SDL_BUTTON_LMASK | SDL_BUTTON_RMASK)) { if(mx <= 150 && my <= 150) { if(mouse_state & SDL_BUTTON_LMASK) { float dx = mx/3 - src_rect.x; float dy = my/3 - src_rect.y; src_rect.x = mx/3; src_rect.y = my/3; src_rect.w -= dx; src_rect.h -= dy; } else if(mouse_state & SDL_BUTTON_RMASK) { src_rect.w = mx/3 - src_rect.x; src_rect.h = my/3 - src_rect.y; } } } GPU_SetUniformf(timeloc, SDL_GetTicks()/1000.0f); GPU_Clear(screen); if(use_color_expansion) GPU_SetAttributeSource(numSprites, color_attr); else GPU_SetAttributeSource(4*numSprites, color_attr); for(i = 0; i < numSprites; i++) { GPU_Rect r = {src_rects[4*i], src_rects[4*i+1], src_rects[4*i+2], src_rects[4*i+3]}; GPU_Blit(image, &r, screen, positions[2*i], positions[2*i+1]); } //GPU_BlitBatchSeparate(image, screen, numSprites, positions, src_rects, expanded_colors, 0); set_shader(0, NULL); GPU_BlitScale(image, NULL, screen, 75, 75, 3.0f, 3.0f); GPU_Rectangle(screen, 3*src_rect.x, 3*src_rect.y, 3*(src_rect.x + src_rect.w), 3*(src_rect.y + src_rect.h), red); GPU_CircleFilled(screen, 3*src_rect.x, 3*src_rect.y, 4, blue); GPU_CircleFilled(screen, 3*(src_rect.x + src_rect.w), 3*(src_rect.y + src_rect.h), 4, green); if(shader_index == 1) set_shader(p, &block); GPU_Flip(screen); frameCount++; if(frameCount%500 == 0) printf("Average FPS: %.2f\n", 1000.0f*frameCount/(SDL_GetTicks() - startTime)); } printf("Average FPS: %.2f\n", 1000.0f*frameCount/(SDL_GetTicks() - startTime)); GPU_FreeImage(image); free_shaders(v, f, p); } GPU_Quit(); return 0; }
void graphics_system_free() { glDeleteBuffers(1, &plainVertices); free_shaders(); algl_system_free(); }
static AlError init_shaders() { BEGIN() plainWidgetShader.shader = NULL; borderWidgetShader.shader = NULL; gridBorderWidgetShader.shader = NULL; modelShader.shader = NULL; textShader.shader = NULL; textShader.texture = NULL; TRY(algl_shader_init_with_sources(&plainWidgetShader.shader, AL_VERT_SHADER(widget), AL_FRAG_SHADER(widget), NULL)); ALGL_GET_UNIFORM(plainWidgetShader, viewportSize); ALGL_GET_UNIFORM(plainWidgetShader, minCoord); ALGL_GET_UNIFORM(plainWidgetShader, size); ALGL_GET_UNIFORM(plainWidgetShader, fillColour); ALGL_GET_ATTRIB(plainWidgetShader, position); TRY(algl_shader_init_with_sources(&borderWidgetShader.shader, AL_VERT_SHADER(widget), AL_FRAG_SHADER(widget), "#define WITH_BORDER\n")); ALGL_GET_UNIFORM(borderWidgetShader, viewportSize); ALGL_GET_UNIFORM(borderWidgetShader, minCoord); ALGL_GET_UNIFORM(borderWidgetShader, size); ALGL_GET_UNIFORM(borderWidgetShader, borderWidth); ALGL_GET_UNIFORM(borderWidgetShader, fillColour); ALGL_GET_UNIFORM(borderWidgetShader, borderColour); ALGL_GET_ATTRIB(borderWidgetShader, position); TRY(algl_shader_init_with_sources(&gridBorderWidgetShader.shader, AL_VERT_SHADER(widget), AL_FRAG_SHADER(widget), "#define WITH_BORDER\n" "#define WITH_GRID")); ALGL_GET_UNIFORM(gridBorderWidgetShader, viewportSize); ALGL_GET_UNIFORM(gridBorderWidgetShader, minCoord); ALGL_GET_UNIFORM(gridBorderWidgetShader, size); ALGL_GET_UNIFORM(gridBorderWidgetShader, borderWidth); ALGL_GET_UNIFORM(gridBorderWidgetShader, gridSize); ALGL_GET_UNIFORM(gridBorderWidgetShader, gridOffset); ALGL_GET_UNIFORM(gridBorderWidgetShader, fillColour); ALGL_GET_UNIFORM(gridBorderWidgetShader, borderColour); ALGL_GET_UNIFORM(gridBorderWidgetShader, gridColour); ALGL_GET_ATTRIB(gridBorderWidgetShader, position); TRY(algl_shader_init_with_sources(&modelShader.shader, AL_VERT_SHADER(model), AL_FRAG_SHADER(model), NULL)); ALGL_GET_UNIFORM(modelShader, viewportSize); ALGL_GET_UNIFORM(modelShader, translate); ALGL_GET_UNIFORM(modelShader, scale); ALGL_GET_UNIFORM(modelShader, colour); ALGL_GET_ATTRIB(modelShader, position); ALGL_GET_ATTRIB(modelShader, param); TRY(algl_shader_init_with_sources(&textShader.shader, AL_VERT_SHADER(text), AL_FRAG_SHADER(text), NULL)); ALGL_GET_UNIFORM(textShader, viewportSize); ALGL_GET_UNIFORM(textShader, minCoord); ALGL_GET_UNIFORM(textShader, size); ALGL_GET_UNIFORM(textShader, charMin); ALGL_GET_UNIFORM(textShader, charSize); ALGL_GET_UNIFORM(textShader, font); ALGL_GET_UNIFORM(textShader, colour); ALGL_GET_UNIFORM(textShader, edge); ALGL_GET_ATTRIB(textShader, position); TRY(algl_texture_init(&textShader.texture)); TRY(algl_texture_load_from_buffer(textShader.texture, images_font_png, images_font_png_size)); fontInfo.numCharsW = 16; fontInfo.numCharsH = 16; fontInfo.charWidth = 0.5; fontInfo.xAdvance = 100.0 / 256.0; fontInfo.edgeCenter = 0.38; fontInfo.edgeSpread = 4.0; CATCH( free_shaders(); )