Browse Source

working ui

master
beta 5 years ago
parent
commit
ac294f64c3
3 changed files with 273 additions and 29 deletions
  1. +1
    -1
      .gitignore
  2. +21
    -3
      includes/game.hpp
  3. +251
    -25
      source/game.cpp

+ 1
- 1
.gitignore View File

@ -35,4 +35,4 @@ bin/
*.dSYM/ *.dSYM/
*.pdb *.pdb
.vcode/ipch
.vscode/ipch

+ 21
- 3
includes/game.hpp View File

@ -24,7 +24,8 @@ enum SHAPE{
TRIANGLE, TRIANGLE,
CIRCLE, CIRCLE,
SQUARE, SQUARE,
PENTAGON
PENTAGON,
UNKOWN
}; };
class Game class Game
{ {
@ -34,11 +35,20 @@ private:
const int SCREEN_WIDTH = 800; const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600; const int SCREEN_HEIGHT = 600;
int MONITOR_WIDTH=1920;
int MONITOR_HEIGHT=1080;
const char *TITLE = "Lesson 01"; const char *TITLE = "Lesson 01";
bool KEEP_ALIVE = true; bool KEEP_ALIVE = true;
bool SHOW_GRID = false; bool SHOW_GRID = false;
SHAPE USER_CURRENT = TRIANGLE; SHAPE USER_CURRENT = TRIANGLE;
SHAPE GUST_CURRENT = UNKOWN;
bool KEY_UP_ARROW_ACTIVE = false;
bool KEY_DOWN_ARROW_ACTIVE = false;
bool KEY_LEFT_ARROW_ACTIVE = false;
bool KEY_RIGHT_ARROW_ACTIVE = false;
bool KEY_SPACE_ACTIVE = false;
bool KEY_RETURN_ACTIVE = false;
bool FULL_SCREEN = false;
public: public:
Game(); Game();
~Game(); ~Game();
@ -50,19 +60,27 @@ public:
void paint(); void paint();
void events( SDL_Event &event ); void events( SDL_Event &event );
void key_down(SDL_Keycode key_code); void key_down(SDL_Keycode key_code);
void key_up(SDL_Keycode key_code);
void resize ( int width, int height ); void resize ( int width, int height );
void triangle(); void triangle();
void triangle(bool filled);
void square(); void square();
void square(bool filled);
void circle(); void circle();
void circle(bool filled);
void pentagon(); void pentagon();
void pentagon(bool filled);
void rectangle(); void rectangle();
void cross(bool filled);
void compass();
void on_key_up_arrow(); void on_key_up_arrow();
void on_key_down_arrow(); void on_key_down_arrow();
void on_key_left_arrow(); void on_key_left_arrow();
void on_key_right_arrow(); void on_key_right_arrow();
void on_key_space(); void on_key_space();
void on_key_enter(); void on_key_enter();
void top_screen(); void top_screen();
void user_screen(); void user_screen();
void guest_screen(); void guest_screen();

+ 251
- 25
source/game.cpp View File

@ -4,11 +4,17 @@ namespace betacore
{ {
Game::Game() Game::Game()
{ {
printf("%d\t%d\n", this->MONITOR_WIDTH,this->MONITOR_HEIGHT);
if (init()) if (init())
{ {
printf("Error loading init\n"); printf("Error loading init\n");
return; return;
} }
// SDL_DisplayMode DM;
// SDL_GetCurrentDisplayMode(0, &DM);
this->MONITOR_HEIGHT=1080;
this->MONITOR_WIDTH=1920;
SDL_Event event; SDL_Event event;
while (this->KEEP_ALIVE) while (this->KEEP_ALIVE)
@ -19,7 +25,9 @@ Game::Game()
} }
close(); close();
} }
Game::~Game() {}
Game::~Game() {
}
int Game::init() int Game::init()
{ {
@ -39,13 +47,16 @@ int Game::init()
//Create window //Create window
int videoFlags = SDL_WINDOW_OPENGL; int videoFlags = SDL_WINDOW_OPENGL;
//videoFlags |= SDL_WINDOW_FULLSCREEN;
videoFlags |= SDL_WINDOW_SHOWN; videoFlags |= SDL_WINDOW_SHOWN;
videoFlags |= SDL_WINDOW_RESIZABLE; videoFlags |= SDL_WINDOW_RESIZABLE;
//videoFlags |= SDL_WINDOW_BORDERLESS;
// videoFlags |= SDL_WINDOW_INPUT_GRABBED;
this->WINDOW = SDL_CreateWindow( this->WINDOW = SDL_CreateWindow(
this->TITLE, this->TITLE,
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
this->SCREEN_WIDTH, this->SCREEN_WIDTH,
this->SCREEN_HEIGHT, this->SCREEN_HEIGHT,
videoFlags); videoFlags);
@ -181,7 +192,15 @@ void Game::grid()
} }
void Game::triangle() void Game::triangle()
{ {
glBegin(GL_POLYGON); // start drawing a polygon
this->triangle(true);
}
void Game::triangle(bool filled)
{
glColor3f(1.0f, 0.0f, 0.0f);
if (filled)
glBegin(GL_POLYGON); // start drawing a polygon
else
glBegin(GL_LINE_LOOP);
glColor3f(1.0f, 0.0f, 0.0f); // Set The Color To Red glColor3f(1.0f, 0.0f, 0.0f); // Set The Color To Red
glVertex3f(0.0f, 1.0f, 0.0f); // Top glVertex3f(0.0f, 1.0f, 0.0f); // Top
glVertex3f(1.0f, -1.0f, 0.0f); // Bottom Right glVertex3f(1.0f, -1.0f, 0.0f); // Bottom Right
@ -190,7 +209,15 @@ void Game::triangle()
} }
void Game::square() void Game::square()
{ {
glBegin(GL_QUADS); // start drawing a polygon (4 sided)
this->square(true);
}
void Game::square(bool filled)
{
glColor3f(0.0f, 0.0f, 1.0f);
if (filled)
glBegin(GL_QUADS); // start drawing a polygon (4 sided)
else
glBegin(GL_LINE_LOOP);
glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left
glVertex3f(1.0f, 1.0f, 0.0f); // Top Right glVertex3f(1.0f, 1.0f, 0.0f); // Top Right
glVertex3f(1.0f, -1.0f, 0.0f); // Bottom Right glVertex3f(1.0f, -1.0f, 0.0f); // Bottom Right
@ -208,7 +235,8 @@ void Game::box()
glVertex3f(-1.0f, -1.0f, 0.0f); // Bottom Left glVertex3f(-1.0f, -1.0f, 0.0f); // Bottom Left
glEnd(); glEnd();
} }
void Game::rectangle(){
void Game::rectangle()
{
glColor3f(1.0f, 1.0f, 1.0f); glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_LINE_LOOP); glBegin(GL_LINE_LOOP);
glVertex3f(-2.5f, 1.0f, 0.0f); // Top Left glVertex3f(-2.5f, 1.0f, 0.0f); // Top Left
@ -217,20 +245,107 @@ void Game::rectangle(){
glVertex3f(-2.5f, -1.0f, 0.0f); // Bottom Left glVertex3f(-2.5f, -1.0f, 0.0f); // Bottom Left
glEnd(); glEnd();
} }
void Game::cross(bool filled){
double width = 0.15;
if (filled)
glBegin(GL_QUADS);
else
glBegin(GL_LINE_LOOP);
glVertex3f(-0.5f, width, 0.0f); // Top Left
glVertex3f(0.5f, width, 0.0f); // Top Right
glVertex3f(0.5f, -width, 0.0f); // Bottom Right
glVertex3f(-0.5f, -width, 0.0f); // Bottom Left
glEnd();
glPushMatrix();
glRotatef(-90.0f, 0.0f, 0.0f, 1.0f);
if (filled)
glBegin(GL_QUADS);
else
glBegin(GL_LINE_LOOP);
glVertex3f(-0.5f, width, 0.0f); // Top Left
glVertex3f(0.5f, width, 0.0f); // Top Right
glVertex3f(0.5f, -width, 0.0f); // Bottom Right
glVertex3f(-0.5f, -width, 0.0f); // Bottom Left
glEnd();
glPopMatrix();
}
void Game::compass()
{
glPushMatrix();
glTranslatef(-1.5f, 0.0f, 0.0f);
glColor3f(1.0f, 1.0f, 1.0f);
this->cross(false);
//MINI TRIANGLE
glPushMatrix();
glScalef(0.12f, 0.12f, 0.0f);
glTranslatef(0.0f, 2.6f, 0.0f);
this->triangle(this->KEY_UP_ARROW_ACTIVE);
glPopMatrix();
// MINI SQUARE
glPushMatrix();
glScalef(0.12f, 0.12f, 0.0f);
glTranslatef(0.0f, -2.6f, 0.0f);
this->square(this->KEY_DOWN_ARROW_ACTIVE);
glPopMatrix();
// MINI PENTAGON
glPushMatrix();
glScalef(0.12f, 0.12f, 0.0f);
glTranslatef(-2.56f, 0.0f, 0.0f);
this->pentagon(this->KEY_LEFT_ARROW_ACTIVE);
glPopMatrix();
// MINI CIRCLE
glPushMatrix();
glScalef(0.12f, 0.12f, 0.0f);
glTranslatef(2.56f, 0.0f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f);
this->circle(this->KEY_RIGHT_ARROW_ACTIVE);
glPopMatrix();
glPushMatrix();
glScalef(0.35f, 0.35f, 0.0f);
glTranslatef(7.0f, 0.0f, 0.0f);
glColor3f(0.0f, 1.0f, 1.0f);
this->circle(this->KEY_SPACE_ACTIVE);
glPopMatrix();
glPushMatrix();
glScalef(0.35f, 0.35f, 0.0f);
glTranslatef(10.0f, 0.0f, 0.0f);
glColor3f(1.0f, 1.0f, 0.0f);
this->circle(this->KEY_RETURN_ACTIVE);
glPopMatrix();
glPopMatrix();
}
void Game::circle() void Game::circle()
{ {
// glBegin(GL_LINE_LOOP);
// for(int i =0; i <= 300; i++){
// double angle = 2 * PI * i / 300;
// double x = cos(angle);
// double y = sin(angle);
// glVertex2d(x,y);
// }
// glEnd();
// this code (mostly) copied from question:
this->circle(true);
}
void Game::circle(bool filled)
{
//glBegin(GL_LINE_LOOP);
// for (int i = 0; i <= 300; i++)
// {
// double angle = 2 * PI * i / 300;
// double x = cos(angle);
// double y = sin(angle);
// glVertex2d(x, y);
// }
// glEnd();
double radius = 1; double radius = 1;
glBegin(GL_POLYGON);
if (filled)
glBegin(GL_POLYGON);
else
glBegin(GL_LINE_LOOP);
double angle1 = 0.0; double angle1 = 0.0;
glVertex2d(radius * cos(0.0), radius * sin(0.0)); glVertex2d(radius * cos(0.0), radius * sin(0.0));
static const int circle_points = 100; static const int circle_points = 100;
@ -245,9 +360,18 @@ void Game::circle()
} }
void Game::pentagon() void Game::pentagon()
{ {
this->pentagon(true);
}
void Game::pentagon(bool filled)
{
glRotatef(-54.0f, 0.0f, 0.0f, 1.0f);
glColor3f(1.0f, 0.0f, 1.0f);
float angleIncrement = 360.0f / 5.0f; float angleIncrement = 360.0f / 5.0f;
angleIncrement *= PI / 180.0f; angleIncrement *= PI / 180.0f;
if ( filled)
glBegin(GL_TRIANGLE_FAN); glBegin(GL_TRIANGLE_FAN);
else
glBegin(GL_LINE_LOOP);
float angle = 0.0f; float angle = 0.0f;
double radius = 1; double radius = 1;
for (int k = 0; k < 100; ++k) for (int k = 0; k < 100; ++k)
@ -270,7 +394,7 @@ void Game::user_screen()
switch (USER_CURRENT) switch (USER_CURRENT)
{ {
case TRIANGLE: case TRIANGLE:
glColor3f(1.0f, 0.0f, 0.0f);
this->triangle(); this->triangle();
break; break;
case CIRCLE: case CIRCLE:
@ -278,12 +402,10 @@ void Game::user_screen()
this->circle(); this->circle();
break; break;
case SQUARE: case SQUARE:
glColor3f(0.0f, 0.0f, 1.0f);
this->square(); this->square();
break; break;
case PENTAGON: case PENTAGON:
glRotatef(-54.0f, 0.0f, 0.0f, 1.0f);
glColor3f(1.0f, 0.0f, 1.0f);
this->pentagon(); this->pentagon();
break; break;
} }
@ -295,18 +417,52 @@ void Game::user_screen()
glPopMatrix(); glPopMatrix();
} }
void Game::guest_screen(){
void Game::guest_screen()
{
glPushMatrix(); glPushMatrix();
glTranslatef(1.5f, -1.0f, -6.0f); glTranslatef(1.5f, -1.0f, -6.0f);
this->box(); this->box();
glTranslatef(1.5f, -1.0f, -6.0f);
// draw a triangle (in smooth coloring mode)
switch (GUST_CURRENT)
{
case TRIANGLE:
this->triangle();
break;
case CIRCLE:
glColor3f(0.0f, 1.0f, 0.0f);
this->circle();
break;
case SQUARE:
this->square();
break;
case PENTAGON:
this->pentagon();
break;
case UNKOWN:
float gray = 105.0 / 255.0;
glColor3f(gray,gray,gray);
glPushMatrix();
glRotatef(-45.0f, 0.0f, 0.0f, 1.0f);
glScalef(2.0f, 2.0f, 0.0f);
this->cross(true);
glPopMatrix();
break;
}
glPopMatrix(); glPopMatrix();
} }
void Game::top_screen(){
void Game::top_screen()
{
glPushMatrix(); glPushMatrix();
glTranslatef(-0.0f, 1.25f, -6.0f); glTranslatef(-0.0f, 1.25f, -6.0f);
this->rectangle(); this->rectangle();
this->compass();
glPopMatrix(); glPopMatrix();
} }
void Game::paint() void Game::paint()
{ {
@ -352,29 +508,99 @@ void Game::key_down(SDL_Keycode key_code)
case SDLK_UP: case SDLK_UP:
printf("UP Key Pressed\n"); printf("UP Key Pressed\n");
this->on_key_up_arrow(); this->on_key_up_arrow();
this->KEY_UP_ARROW_ACTIVE = true;
break; break;
case SDLK_DOWN: case SDLK_DOWN:
printf("Down Key Pressed\n"); printf("Down Key Pressed\n");
this->on_key_down_arrow(); this->on_key_down_arrow();
this->KEY_DOWN_ARROW_ACTIVE = true;
break; break;
case SDLK_LEFT: case SDLK_LEFT:
printf("Left Key Pressed\n"); printf("Left Key Pressed\n");
this->on_key_left_arrow(); this->on_key_left_arrow();
this->KEY_LEFT_ARROW_ACTIVE = true;
break; break;
case SDLK_RIGHT: case SDLK_RIGHT:
printf("Right Key Pressed\n"); printf("Right Key Pressed\n");
this->on_key_right_arrow(); this->on_key_right_arrow();
this->KEY_RIGHT_ARROW_ACTIVE = true;
break; break;
case SDLK_g: case SDLK_g:
this->SHOW_GRID = !SHOW_GRID; this->SHOW_GRID = !SHOW_GRID;
break; break;
case SDLK_SPACE: case SDLK_SPACE:
printf("Space Key Pressed\n");
this->KEY_SPACE_ACTIVE = true;
break; break;
case SDLK_RETURN:
this->KEY_RETURN_ACTIVE = true;
break;
case SDLK_F11:
if(FULL_SCREEN){
SDL_SetWindowPosition(this->WINDOW, 100, 100);
SDL_RestoreWindow(this->WINDOW);
//SDL_SetWindowResizable(this->WINDOW, SDL_TRUE);
//SDL_SetWindowBordered(this->WINDOW, 1);
SDL_SetWindowSize(this->WINDOW,SCREEN_WIDTH, SCREEN_HEIGHT);
//SDL_SetWindowFullscreen(this->WINDOW,SDL_WINDOW_FULLSCREEN );
}
else {
SDL_SetWindowPosition(this->WINDOW, 0, 0);
//SDL_SetWindowResizable(this->WINDOW, SDL_FALSE);
//SDL_SetWindowBordered(this->WINDOW, 0);
SDL_SetWindowSize(this->WINDOW,MONITOR_WIDTH, MONITOR_HEIGHT);
//SDL_SetWindowFullscreen(this->WINDOW,0 );
}
this->FULL_SCREEN = !this->FULL_SCREEN;
SDL_ShowCursor(this->FULL_SCREEN);
break;
default:
//Do nothing
break;
}
}
/**
* key down
* SDL_Keycode key_code
*/
void Game::key_up(SDL_Keycode key_code)
{
switch (key_code)
{
case SDLK_UP:
this->KEY_UP_ARROW_ACTIVE = false;
break;
case SDLK_DOWN:
this->KEY_DOWN_ARROW_ACTIVE = false;
break;
case SDLK_LEFT:
this->KEY_LEFT_ARROW_ACTIVE = false;
break;
case SDLK_RIGHT:
this->KEY_RIGHT_ARROW_ACTIVE = false;
break;
case SDLK_SPACE:
this->KEY_SPACE_ACTIVE = false;
break;
case SDLK_RETURN:
this->KEY_RETURN_ACTIVE = false;
break;
case SDLK_F11:
// SDL_DisplayMode dm;
// SDL_RestoreWindow(this->WINDOW); //Incase it's maximized...
// SDL_SetWindowSize(this->WINDOW, dm.w, dm.h + 10);
// SDL_SetWindowPosition(this->WINDOW, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
default: default:
//Do nothing //Do nothing
break; break;
@ -415,7 +641,7 @@ void Game::events(SDL_Event &event)
break; break;
case SDL_KEYUP: case SDL_KEYUP:
//Todo Key up commands
this->key_up(event.key.keysym.sym);
break; break;
} //end switch(event.type) } //end switch(event.type)

Loading…
Cancel
Save