GPU_ShaderBlock load_shaders(Uint32* v, Uint32* f, Uint32* p) { *v = load_shader(GPU_VERTEX_SHADER, "data/shaders/time_mod.vert"); if(!*v) GPU_LogError("Failed to load vertex shader: %s\n", GPU_GetShaderMessage()); *f = load_shader(GPU_FRAGMENT_SHADER, "data/shaders/time_mod.frag"); if(!*f) GPU_LogError("Failed to load fragment shader: %s\n", GPU_GetShaderMessage()); *p = GPU_LinkShaders(*v, *f); if(!*p) { GPU_ShaderBlock b = {-1, -1, -1, -1}; GPU_LogError("Failed to link shader program: %s\n", GPU_GetShaderMessage()); return b; } { GPU_ShaderBlock block = GPU_LoadShaderBlock(*p, "gpu_Vertex", "gpu_TexCoord", "gpu_Color", "gpu_ModelViewProjectionMatrix"); GPU_ActivateShaderProgram(*p, &block); return block; } }
shader_error::shader_error(const std::string &op) : game::error(op + "\n" + GPU_GetShaderMessage()) { std::cerr << GPU_GetShaderMessage() << std::endl; }
int main(int argc, char* argv[]) { GPU_Target* screen; GPU_SetDebugLevel(GPU_DEBUG_LEVEL_MAX); SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); GPU_SetRequiredFeatures(GPU_FEATURE_BASIC_SHADERS); screen = GPU_InitRenderer(GPU_RENDERER_OPENGL_3, 800, 600, GPU_DEFAULT_INIT_FLAGS); if(screen == NULL) { GPU_LogError("Initialization Error: Could not create a renderer with proper feature support for this demo.\n"); return 1; } glewExperimental = GL_TRUE; // Force GLEW to get exported functions instead of checking via extension string if(glewInit() != GLEW_OK) { GPU_LogError("Initialization Error: Failed to initialize GLEW.\n"); return 2; } { GPU_Image* image; float dt; Uint32 startTime; long frameCount; int maxSprites = 50; int numSprites; float x[50]; float y[50]; float velx[50]; float vely[50]; int i; Uint8 done; SDL_Event event; image = GPU_LoadImage("data/test.bmp"); if (image == NULL) return 3; v = GPU_LoadShader(GPU_VERTEX_SHADER, "data/shaders/untextured-150.vert"); f = GPU_LoadShader(GPU_FRAGMENT_SHADER, "data/shaders/untextured-150.frag"); p = GPU_LinkShaders(v, f); GPU_Log("%s\n", GPU_GetShaderMessage()); glUseProgram(p); vertex_loc = GPU_GetAttributeLocation(p, "gpu_Vertex"); color_loc = GPU_GetAttributeLocation(p, "gpu_Color"); modelViewProjection_loc = GPU_GetUniformLocation(p, "gpu_ModelViewProjectionMatrix"); glGenVertexArrays(1, &VAO); glBindVertexArray(VAO); glGenBuffers(1, &VBO); glBindBuffer(GL_ARRAY_BUFFER, VBO); dt = 0.010f; startTime = SDL_GetTicks(); frameCount = 0; numSprites = 1; for (i = 0; i < maxSprites; 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 < maxSprites) numSprites++; } else if (event.key.keysym.sym == SDLK_MINUS) { if (numSprites > 0) numSprites--; } } } 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_Clear(screen); draw_3d_stuff(screen); for (i = 0; i < numSprites; i++) { GPU_Blit(image, NULL, screen, x[i], y[i]); } draw_more_3d_stuff(screen); 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); } GPU_Quit(); return 0; }