Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "MainGameSteps.h"
- #include "SDL.h"
- #include "SDL_opengl.h"
- #include "GameWindow.h"
- #include <cmath>
- #include <iostream>
- #include <cstdlib>
- using namespace std;
- MainGameSteps::MainGameSteps() : exit(false) {}
- void MainGameSteps::setup()
- {
- degreesToRotateX = degreesToRotateY = degreesToRotateZ = 0;
- camX = camY = 0;
- camZ = 5;
- glEnable(GL_BLEND);
- glEnable(GL_CULL_FACE);
- glEnable(GL_DEPTH_TEST);
- glEnable(GL_POLYGON_SMOOTH);
- // glDepthFunc(GL_ALWAYS);
- glDepthRange(100,-100);
- glCullFace(GL_BACK);
- if(!glIsEnabled(GL_DEPTH_TEST))
- std::exit(-1);
- }
- void MainGameSteps::processEvents(const SDL_Event& event)
- {
- switch (event.type)
- {
- case SDL_QUIT:
- exit = true;
- break;
- case SDL_KEYDOWN:
- switch (event.key.keysym.sym)
- {
- case SDLK_F4:
- if (event.key.keysym.sym == SDLK_F4
- && (event.key.keysym.mod & KMOD_ALT))
- exit = true;
- break;
- case SDLK_SPACE:
- degreesToRotateX = degreesToRotateY = degreesToRotateZ = 0;
- break;
- case SDLK_1:
- glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
- break;
- case SDLK_2:
- glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
- break;
- case SDLK_3:
- glPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
- break;
- default:
- break;
- }
- break;
- }
- }
- void MainGameSteps::processLogics()
- {
- //Distância para girar (em graus) =
- //velocidade (0.180f) * tempo (ticks)
- float distance = 0.180f * GAMEWINDOW.getTicks();
- degreesToRotateY += distance;
- }
- #define CUBE_SIZE_2 .5
- static void drawCube()
- {
- glBegin(GL_TRIANGLES);
- //Frente
- glVertex3f(CUBE_SIZE_2, CUBE_SIZE_2, CUBE_SIZE_2);
- glVertex3f(-CUBE_SIZE_2, CUBE_SIZE_2, CUBE_SIZE_2);
- glVertex3f(-CUBE_SIZE_2, -CUBE_SIZE_2, CUBE_SIZE_2);
- glVertex3f(-CUBE_SIZE_2, -CUBE_SIZE_2, CUBE_SIZE_2);
- glVertex3f(CUBE_SIZE_2, -CUBE_SIZE_2, CUBE_SIZE_2);
- glVertex3f(CUBE_SIZE_2, CUBE_SIZE_2, CUBE_SIZE_2);
- //Verso
- glVertex3f(CUBE_SIZE_2, CUBE_SIZE_2, -CUBE_SIZE_2);
- glVertex3f(CUBE_SIZE_2, -CUBE_SIZE_2, -CUBE_SIZE_2);
- glVertex3f(-CUBE_SIZE_2, -CUBE_SIZE_2, -CUBE_SIZE_2);
- glVertex3f(-CUBE_SIZE_2, -CUBE_SIZE_2, -CUBE_SIZE_2);
- glVertex3f(-CUBE_SIZE_2, CUBE_SIZE_2, -CUBE_SIZE_2);
- glVertex3f(CUBE_SIZE_2, CUBE_SIZE_2, -CUBE_SIZE_2);
- // //Direita
- glVertex3f(CUBE_SIZE_2, CUBE_SIZE_2, CUBE_SIZE_2);
- glVertex3f(CUBE_SIZE_2, -CUBE_SIZE_2, CUBE_SIZE_2);
- glVertex3f(CUBE_SIZE_2, -CUBE_SIZE_2, -CUBE_SIZE_2);
- glVertex3f(CUBE_SIZE_2, -CUBE_SIZE_2, -CUBE_SIZE_2);
- glVertex3f(CUBE_SIZE_2, CUBE_SIZE_2, -CUBE_SIZE_2);
- glVertex3f(CUBE_SIZE_2, CUBE_SIZE_2, CUBE_SIZE_2);
- // //Esquerda
- glVertex3f(-CUBE_SIZE_2, -CUBE_SIZE_2, -CUBE_SIZE_2);
- glVertex3f(-CUBE_SIZE_2, -CUBE_SIZE_2, CUBE_SIZE_2);
- glVertex3f(-CUBE_SIZE_2, CUBE_SIZE_2, CUBE_SIZE_2);
- glVertex3f(-CUBE_SIZE_2, CUBE_SIZE_2, CUBE_SIZE_2);
- glVertex3f(-CUBE_SIZE_2, CUBE_SIZE_2, -CUBE_SIZE_2);
- glVertex3f(-CUBE_SIZE_2, -CUBE_SIZE_2, -CUBE_SIZE_2);
- // //Baixo
- glVertex3f(-CUBE_SIZE_2, -CUBE_SIZE_2, -CUBE_SIZE_2);
- glVertex3f(CUBE_SIZE_2, -CUBE_SIZE_2, -CUBE_SIZE_2);
- glVertex3f(CUBE_SIZE_2, -CUBE_SIZE_2, CUBE_SIZE_2);
- glVertex3f(CUBE_SIZE_2, -CUBE_SIZE_2, CUBE_SIZE_2);
- glVertex3f(-CUBE_SIZE_2, -CUBE_SIZE_2, CUBE_SIZE_2);
- glVertex3f(-CUBE_SIZE_2, -CUBE_SIZE_2, -CUBE_SIZE_2);
- // //Abaixo
- glVertex3f(CUBE_SIZE_2, CUBE_SIZE_2, CUBE_SIZE_2);
- glVertex3f(CUBE_SIZE_2, CUBE_SIZE_2, -CUBE_SIZE_2);
- glVertex3f(-CUBE_SIZE_2, CUBE_SIZE_2, -CUBE_SIZE_2);
- glVertex3f(-CUBE_SIZE_2, CUBE_SIZE_2, -CUBE_SIZE_2);
- glVertex3f(-CUBE_SIZE_2, CUBE_SIZE_2, CUBE_SIZE_2);
- glVertex3f(CUBE_SIZE_2, CUBE_SIZE_2, CUBE_SIZE_2);
- glEnd();
- }
- void MainGameSteps::draw() const
- {
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity(); //Resetamos a projeção atual
- gluPerspective(60, GAMEWINDOW.getRatio(), 1, 15000);
- glMatrixMode(GL_MODELVIEW);
- glPushMatrix();
- //Limpa a tela
- gluLookAt(camX, camY, camZ, 0, 0, 0, 0, 1, 0);
- glClearColor(0.0, 0.0, 0.0, 0.0);
- // glClearDepth(1.0);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- // glTranslatef(0,0, -250);
- // //Gira o triângulo
- // glRotatef(degreesToRotateZ, 0,0,1);
- // glRotatef(degreesToRotateX, 1,0,0);
- // glRotatef(degreesToRotateY, 0,1,0);
- //Sun
- glColor3f(1, 1, .8f);
- drawCube();
- glPushMatrix();
- //Planet
- glRotatef(degreesToRotateY, 0,1,0);
- glColor3f(0, .2, .8f);
- glTranslatef(0, 0, -4);
- glScalef(.4f, .4f, .4f);
- glRotatef(degreesToRotateY*2, 0,1,0);
- drawCube();
- glPopMatrix();
- glPopMatrix();
- }
- bool MainGameSteps::ended()
- {
- return exit;
- }
- void MainGameSteps::teardown() {}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement