void Graphics::render() { if (!start()) return; if (!initialized) { error->log(GRAPHICS, IMPORTANT, "Render function called without graphics initialization\n"); return; } // I feel like most of this should not get called every frame, but whatever GLfloat mat_specular[]={ .2, .2, .2, 1.0 }; glShadeModel(GL_SMOOTH); glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular ); glEnable(GL_CULL_FACE); glCullFace(GL_BACK); glEnable(GL_COLOR_MATERIAL); glEnable(GL_DEPTH_TEST); if (Scheduler::getInstance().raceState == SETUP) { glClearColor(0.0,0.0,0.0,0.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); World::getInstance().setupMenu->draw(); renderHUD(); } else { if(glowEnabled) { glViewport(0,0,glowTexWidth,glowTexHeight); renderGlowLayer(); World &world = World::getInstance(); glViewport(0,0,world.camera.getWres(),world.camera.getHres()); copyBufferToTexture(); renderColorLayer(); // Composite; renderGlowTexture(); renderHUD(); } else { World &world = World::getInstance(); glViewport(0,0,world.camera.getWres(),world.camera.getHres()); renderColorLayer(); renderHUD(); } } SDL_GL_SwapBuffers(); finish(); }
//finally here. Whew that's a lot of functions int main(int argc,char* args[]) { srand(time(NULL)); //spin the wheel! int i; //loop counter int frame=0; //total frames past if(init()==false) return 1; if(prepAssets()==false) return 1; if(Mix_PlayMusic(muBGM,-1)==-1) return 1; //read and store the string of appropriate language FILE *pLang; char strLang[25]; //language string if((pLang=fopen("text/en.WhyCantIHoldAllTheseFileExtensions","r"))!=NULL) { if(fgets(strLang,25,pLang)==NULL) return 1; } fclose(pLang); sfMenuPrompt=TTF_RenderText_Blended(fnMenu,strLang,clMenu); //read and store current highscore FILE *pHighScoreR; char strHighScore[10]; if((pHighScoreR=fopen("text/highscore.WhyCantIHoldAllTheseFileExtensions","r"))!=NULL) { if(fgets(strHighScore,10,pHighScoreR)==NULL) return 1; } fclose(pHighScoreR); sfHighScore=TTF_RenderText_Shaded(fnHighScore,strHighScore,clHighScore,clDefault); iHighScore=atoi(strHighScore); //string contents as an int //menu runs here while(quitMenu==false) { //display menu printb(0,0,sfMenu,sfScreen); printb((SCREEN_WIDTH-sfMenuPrompt->w)/2,315,sfMenuPrompt,sfScreen); //menu-only key controls while(SDL_PollEvent(&event)) { if(event.type==SDL_KEYDOWN) { switch(event.key.keysym.sym) { case SDLK_RETURN: quitMenu=true; break; case SDLK_ESCAPE: quitMenu=true; quitGame=true; quitOver=true; quitAll=true; break; default: ; } } //if the window gets X'd if(event.type==SDL_QUIT) { quitMenu=true; //quit the menu quitGame=true; //skip the game quitOver=true; quitAll=true; } } //refresh the screen if(SDL_Flip(sfScreen)==-1) return 1; } //game is starting! set up everything! tmTime.start(); tmFPS.start(); tmFPSUpd.start(); tmDelta.start(); tmMusic.start(); //REPLAY LOOP while(quitAll==false) { randBullets(); //game runs here while(quitGame==false) { //once wave time is up: level up and restart wave timer //setup phase is active if(waveZero==true&&tmTime.getTicks()>10000) { iWave++; nextWave(); tmTime.start(); waveZero=false; newBGM(); tmScore.start(); tmTimeAlive.start(); } //setup phase is off else if(tmTime.getTicks()>WAVE_LENGTH) { iWave++; nextWave(); tmTime.start(); } //change music after 90 seconds if(tmMusic.getTicks()>90000) { newBGM(); tmMusic.start(); } //score acceleration if(tmTimeAlive.getTicks()>30000) iScoreAccel=13; else if(tmTimeAlive.getTicks()>15000) iScoreAccel=6; else if(tmTimeAlive.getTicks()>7500) iScoreAccel=3; else if(tmTimeAlive.getTicks()>0) iScoreAccel=1; //score timing if(tmScore.getTicks()>250) { iScore+=iScoreAccel; tmScore.start(); } //1up timing if(tmTimeAlive.getTicks()>45000) { tmTimeAlive.start(); iLife++; //don't let player have too many lives //if 1up is allowed, play sound if(iLife>5)iLife=5; else if(Mix_PlayChannel(-1,chGain,0)==-1) return 1; } //while there's science to do while(SDL_PollEvent(&event)) { //ship controls myship.handleInput(); //other controls if(event.type==SDL_KEYDOWN) { switch(event.key.keysym.sym) { case SDLK_ESCAPE: quitGame=true; quitOver=true; quitAll=true; break; case SDLK_x: if(useBomb()==false) return 1; break; default: ; } } //if the window gets X'd if(event.type == SDL_QUIT) { quitGame = true; quitOver=true; quitAll=true; } } //update screen data myship.move(tmDelta.getTicks()); //update ship's position tmDelta.start(); //restart change of time timer printb(0,0,sfBG,sfScreen); //print background myship.show(); //print position to screen if(diedRecently==true) printb(120,0,sfDeathOverlay,sfScreen,NULL); if(bombedRecently==true) printb(120,0,sfBombFlash,sfScreen,NULL); if(waveZero==true) { //reset bullets to original when looping game printb(0,0,sfHowTo,sfScreen,NULL); iMaxBul=-1; } for(i=0; i<=iMaxBul; i++) { //player has died: do all relevant tracking if(isCol(myship.hitbox,b[i].hitbox)) { iLife--; iBomb=3; iScore-=50; if(iLife==0) quitGame=true; diedRecently=true; b[i].hitbox.x=rand()%420-120; b[i].hitbox.y=0; tmDeathOverlay.start(); tmTimeAlive.start(); if(Mix_PlayChannel(-1,chDeath,0)==-1) return 1; } if(b[i].hitbox.x>515) b[i].hitbox.x=120; if(b[i].hitbox.x<120) b[i].hitbox.x=515; //compensate for bullet width if(b[i].hitbox.y>480) { //because collision is counted from sScore of the picture b[i].hitbox.y=0; //so bulletwidth had to be subtracted b[i].xVel=rand()%5-2; //bullet can travel left or right b[i].yVel=rand()%4+1; //can only travel down } b[i].hitbox.y+=b[i].yVel; b[i].hitbox.x+=b[i].xVel; printb(b[i].hitbox.x,b[i].hitbox.y,sfBullet,sfScreen,NULL); } //expiry dates for death and bomb notifications if(tmDeathOverlay.getTicks()>500) diedRecently=false; if(tmBombFlash.getTicks()>250) bombedRecently=false; //display all stats renderHUD(); printb(7,50,sfHighScore,sfScreen,NULL); //refresh the screen if(SDL_Flip(sfScreen)==-1) return 1; //limit the frame rate if(tmFPS.getTicks()<1000/FRAMES_PER_SECOND) { SDL_Delay((1000/FRAMES_PER_SECOND)-tmFPS.getTicks()); tmFPS.start(); } frame++; //one frame has passed //update this once per second if(tmFPSUpd.getTicks()>1000) { std::stringstream newCaption; newCaption<<frame/(tmFPS.getTicks()/1000.f)<<" fps"; SDL_WM_SetCaption(newCaption.str().c_str(),NULL); tmFPSUpd.start(); //restart for the next one-second wait } } //store new high score, if there is one if(iScore>iHighScore) { FILE *pHighScoreW; if((pHighScoreW=fopen("text/highscore.WhyCantIHoldAllTheseFileExtensions","w"))!=NULL) { if(fprintf(pHighScoreW,"%d",iScore)==0) return 1; } fclose(pHighScoreW); newHighScore=true; } //stop playing music Mix_HaltMusic(); //game over runs here while(quitOver==false) { //some key events while(SDL_PollEvent(&event)) { if(event.type==SDL_KEYDOWN) { switch(event.key.keysym.sym) { case SDLK_RETURN: quitOver=true; break; case SDLK_ESCAPE: quitOver=true; quitAll=true; break; default: ; } } //if the window gets X'd if(event.type == SDL_QUIT) { quitOver=true; quitAll=true; } } //end surfaces std::stringstream finalScore; finalScore<<iScore; sfScore=TTF_RenderText_Blended(fnFinalScore,finalScore.str().c_str(),clMenu); //display restart prompt FILE *pRestart; char strRestart[30]; if((pRestart=fopen("text/enr.WhyCantIHoldAllTheseFileExtensions","r"))!=NULL) { if(fgets(strRestart,30,pRestart)==NULL) return 1; } fclose(pRestart); sfRestart=TTF_RenderText_Blended(fnMenu,strRestart,clScore); //print everything printb(0,0,sfOverBG,sfScreen,NULL); printb((SCREEN_WIDTH-sfRestart->w)/2,385,sfRestart,sfScreen,NULL); printb((SCREEN_WIDTH-sfScore->w)/2,240,sfScore,sfScreen,NULL); if(newHighScore==true) printb(430,280,sfNewHigh,sfScreen,NULL); //refresh the screen if(SDL_Flip(sfScreen)==-1) return 1; SDL_WM_SetCaption("Shutengu!!",NULL); } //reset loop conditions to allow replaying resetGame(); } //user has now quit cleanUp(); return 0; }
void renderUniverse(struct renderstate *render, struct simulation *sim, struct universe *univ) { // SDL_Rect rect; int i; int rb, g; double maxCharge = 0; for (i = 0; i < univ->nextParticle; i++) { if (univ->particles[i].isActive && fabs(univ->particles[i].charge) > maxCharge) { maxCharge = fabs(univ->particles[i].charge); } } SDL_SetRenderDrawColor(render->renderer, 0, 0, 0, 255); SDL_RenderClear(render->renderer); // FPS render->frames++; if ((SDL_GetTicks() - render->last_frame) > 1000) { render->fps = render->frames; render->frames = 0; render->last_frame += 1000; } // render->point_count = 0; for (i = 0; i < univ->nextParticle; i++) { if (!univ->particles[i].isActive) { continue; } double x = (((univ->particles[i].xPos * univ->scale) + render->xPos) * render->scale) + (render->width / 2); double y = (((univ->particles[i].yPos * univ->scale) + render->yPos) * render->scale) + (render->height / 2); double size = (int)(univ->particles[i].size * render->scale * univ->scale); if (size < 1) { size = 1; } if (univ->particles[i].charge == 0) { g = 255; rb = 0; } else if (fabs(univ->particles[i].charge) < maxCharge / 2.0) { g = 255; rb = (int)(255.0 * (fabs(univ->particles[i].charge) / (maxCharge / 2.0))); } else { rb = 255; g = (int)(255.0 * ((maxCharge - fabs(univ->particles[i].charge)) / (maxCharge / 2.0))); } if (univ->particles[i].charge < 0) { SDL_SetRenderDrawColor(render->renderer, 0, g, rb, 255); renderCircle(render, x, y, size); } else { SDL_SetRenderDrawColor(render->renderer, rb, g, 0, 255); renderCircle(render, x, y, size); } // SDL_RenderFillRect(render->renderer, &rect); } // SDL_RenderDrawPoints(render->renderer, render->points, // render->point_count); renderHotParticle(render, sim, univ); renderHUD(render, sim, univ); SDL_RenderPresent(render->renderer); }
void Perlin3DViewerWidget::paintGL() { int width = this->width(); int height = this->height(); glViewport( 0, 0, width / 4 * 3, height ); /// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); /// glUniformMatrix4fvARB( _shader._location_u_modelviewMatrix, 1, GL_FALSE, _viewMatrix.Ptr() ); glUniformMatrix4fvARB( _shader._location_u_projectionMatrix, 1, GL_FALSE, _projectionMatrix.Ptr() ); { // HULL glUniform4f( _shader._location_u_mode, 1, 1, 1, 0 ); bool lightEnabled = Navigator_GlobalValue::pTest->_lightEnabled; glUniform1i( _shader._location_u_lightEnabled, lightEnabled ? 1 : 0 ); if (Navigator_GlobalValue::pTest->_polyEnabled) { for (Perlin3D_Chunk* element : _Perlin3D_Chunks) if (element->isEnabled() && element->isVisible( _Frustum )) element->render(); } if (lightEnabled) glUniform1i( _shader._location_u_lightEnabled, 0 ); glDepthFunc(GL_LEQUAL); glUniform4f( _shader._location_u_mode, 0, 0, 0, 1 ); if (Navigator_GlobalValue::pTest->_lineEnabled) { for (Perlin3D_Chunk* element : _Perlin3D_Chunks) if (element->isEnabled() && element->isVisible( _Frustum )) element->render_lines(); } glDepthFunc(GL_LESS); } // /HULL { // BOX glUniformMatrix4fvARB( _shader._location_u_projectionMatrix, 1, GL_FALSE, _projectionMatrix.Ptr() ); renderBoxes( _viewMatrix ); } // /BOX { // HUD glClear(GL_DEPTH_BUFFER_BIT); renderHUD(); } // /HUD glFlush(); glCheckError(); }
int main(int argc, char* args[]) { //quit flag bool quit = false; //frame rate regulator Timer fps; if(init() == false) return 1; if(load_files() == false) return 1; setButtons_and_Frames(); create_Tooltips(); //start frame counter frame = 0; //play music //Mix_PlayMusic(mainMusic, -1); //the player Player *player0 = NULL; //while in game while(quit == false) { //start frame timer fps.start(); //while events to handle while(SDL_PollEvent(&event)) { //player0 ship movement/ shooting if(isPaused == false) player0->handle_input(); else quit = handle_menu_input(); //if user closes window if(event.type == SDL_QUIT) { quit = true; } } if(isPaused == false) { //background apply_surface(0, 0, background, screen, &camera); if(gameMode == 0) { doMainGame(player0); } else if(gameMode == 1) doArcadeMode(player0); //control units player0->doUnit(); doGrunts(); doBoomers(); doStealths(); doCarriers(); doExplosions(); //HUD renderHUD(player0); } else if(quit == false) //paused, show some sort of menu; { switch(menu) //show menu based on menu variable { case 0: quit = doMainMenu(); //going to mainMenu, reset everything if(player0 != NULL) delete player0; player0 = new Player; reset(); break; case 1: instructionsMenu(); break; case 2: doPauseMenu(); break; case 3: doSkillMenu(player0); break; case 4: gameOverMenu(); break; //case 5: doShopMenu(player0); break; //if not valid menu, unpause game case 6: victoryMenu(); break; default: isPaused = false; } } //update screen if(SDL_Flip(screen) == -1) return 1; //cap frame rate if(fps.get_ticks() < 1000 / FRAMES_PER_SECOND) { SDL_Delay((1000 / FRAMES_PER_SECOND) - fps.get_ticks()); } frame++; } clean_up(); return 0; }