Original Post
So I am finally transitioning from console based programming into event-driven / 2d gfx programming. I have been looking through lazyfoos tutorials and they have helped a ton. I started to make a Tic-Tac-Toe game yesterday and it started off pretty good, I think. The problem I am running into right now is how to define if it is playerOne's turn or not.
I am using a bool for playerOne, true means it is playerOnes turn, false means it isn't. For now there are not actually 2 players, but I am trying to make it so when it playerOne == true, the box turns blue when clicked, and when playerOne == false, turn red. Later on I will switch it to images, but I want the logic down.
I suppose it will be easier if I post the code so here it is. Any help will be greatly appreciated:
[source lang="cpp"]#include "SDL.h"
#include "SDL_opengl.h"
#include "SDL_image.h"
#include
#include
const int HOLDERS = 9; // Number of holders for images
void setupWindow()
{
// Initialize SDL
SDL_Init(SDL_INIT_EVERYTHING);
// Allocate memory for GL
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
// Caption on the window pane
SDL_WM_SetCaption ("Tic-Tac-Toe", NULL);
// Setup the window
SDL_SetVideoMode(600, 600, 32, SDL_OPENGL);
// Clears the screen to this color
glClearColor(1,1,1,1);
// What is displayed of the screen
glViewport(0,0,600,600);
// Smooth color transition
glShadeModel(GL_SMOOTH);
// Sets to 2D
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Disable depth test since not 3D
glDisable(GL_DEPTH_TEST);
}
struct Holder
{
float xPos;
float yPos;
float width;
float height;
bool isVacant;
};
int main (int argc, char* args[])
{
// Function for window setup
setupWindow();
/*---------------------------------------------------------------------------------------------------*/
/*--------------------------- VARIABLE SETUP -----------------------------------------------------*/
/*---------------------------------------------------------------------------------------------------*/
// Handles game loop
bool isRunning = true;
bool playerOne = true;
// Handles SDL events
SDL_Event event;
// Array of Holders type
Holder holders[HOLDERS];
// Setup the holders
for (int a = 0, x = 20, y = 20; a < HOLDERS; a++, x+= 190)
{
if ( x > 400)
{
x = 20;
y += 190;
}
holders[a].xPos = x;
holders[a].yPos = y;
holders[a].width = 180;
holders[a].height = 180;
holders[a].isVacant = true;
}
/*---------------------------------------------------------------------------------------------------*/
/*--------------------------- MAIN LOOP -----------------------------------------------------*/
/*---------------------------------------------------------------------------------------------------*/
// Game loop
while (isRunning)
{
/*---------------------------------------------------------------------------------------------------*/
/*--------------------------- 1. EVENTS --------------------------------------------------*/
/*---------------------------------------------------------------------------------------------------*/
while (SDL_PollEvent(&event))
{
// If window is closed
if (event.type == SDL_QUIT)
{
isRunning = false;
}
// If escape key is pressed
if (event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_ESCAPE)
{
isRunning = false;
}
// Mouse click
if (event.type == SDL_MOUSEBUTTONDOWN)
{
if (event.button.button == SDL_BUTTON_LEFT)
{
// Mouse offsets
int x = 0, y = 0;
x = event.button.x;
y = event.button.y;
for (int a = 0; a < HOLDERS; a++)
{
if ((x > holders[a].xPos) && (x < holders[a].xPos + holders[a].width) &&
(y > holders[a].yPos) && (y < holders[a].yPos + holders[a].height) && holders[a].isVacant == true)
{
OutputDebugString("Spot no longer vacant\n");
holders[a].isVacant = false;
}
}
}
}
}
/*---------------------------------------------------------------------------------------------------*/
/*--------------------------- 2. LOGIC -----------------------------------------------------*/
/*---------------------------------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------------------------------*/
/*--------------------------- 3. RENDER -----------------------------------------------------*/
/*---------------------------------------------------------------------------------------------------*/
// Clears screen before rendering
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix(); // Start phase
glOrtho(0,600,600,0,-1,1); // Set the matrix
// THE HOLDERS
glBegin(GL_QUADS);
// Used to display vacant spaces black
for (int n = 0; n < HOLDERS; n++)
{
if (holders[n].isVacant == true)
{
glColor4ub(0,0,0,255);
glVertex2f(holders[n].xPos, holders[n].yPos);
glVertex2f(holders[n].xPos + holders[n].width, holders[n].yPos);
glVertex2f(holders[n].xPos + holders[n].width, holders[n].yPos + holders[n].height);
glVertex2f(holders[n].xPos, holders[n].yPos + holders[n].height);
}
else if (holders[n].isVacant == false && playerOne == true)
{
glColor4ub(0,0,255,255);
glVertex2f(holders[n].xPos, holders[n].yPos);
glVertex2f(holders[n].xPos + holders[n].width, holders[n].yPos);
glVertex2f(holders[n].xPos + holders[n].width, holders[n].yPos + holders[n].height);
glVertex2f(holders[n].xPos, holders[n].yPos + holders[n].height);
}
else if (holders[n].isVacant == false && playerOne == false)
{
glColor4ub(255,0,0,255);
glVertex2f(holders[n].xPos, holders[n].yPos);
glVertex2f(holders[n].xPos + holders[n].width, holders[n].yPos);
glVertex2f(holders[n].xPos + holders[n].width, holders[n].yPos + holders[n].height);
glVertex2f(holders[n].xPos, holders[n].yPos + holders[n].height);
}
}
glEnd();
glPopMatrix(); // End phase
// Draws everything above it to the screen
SDL_GL_SwapBuffers();
}
SDL_Quit;
return 0;
}[/source]
I am using a bool for playerOne, true means it is playerOnes turn, false means it isn't. For now there are not actually 2 players, but I am trying to make it so when it playerOne == true, the box turns blue when clicked, and when playerOne == false, turn red. Later on I will switch it to images, but I want the logic down.
I suppose it will be easier if I post the code so here it is. Any help will be greatly appreciated:
[source lang="cpp"]#include "SDL.h"
#include "SDL_opengl.h"
#include "SDL_image.h"
#include
#include
const int HOLDERS = 9; // Number of holders for images
void setupWindow()
{
// Initialize SDL
SDL_Init(SDL_INIT_EVERYTHING);
// Allocate memory for GL
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
// Caption on the window pane
SDL_WM_SetCaption ("Tic-Tac-Toe", NULL);
// Setup the window
SDL_SetVideoMode(600, 600, 32, SDL_OPENGL);
// Clears the screen to this color
glClearColor(1,1,1,1);
// What is displayed of the screen
glViewport(0,0,600,600);
// Smooth color transition
glShadeModel(GL_SMOOTH);
// Sets to 2D
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Disable depth test since not 3D
glDisable(GL_DEPTH_TEST);
}
struct Holder
{
float xPos;
float yPos;
float width;
float height;
bool isVacant;
};
int main (int argc, char* args[])
{
// Function for window setup
setupWindow();
/*---------------------------------------------------------------------------------------------------*/
/*--------------------------- VARIABLE SETUP -----------------------------------------------------*/
/*---------------------------------------------------------------------------------------------------*/
// Handles game loop
bool isRunning = true;
bool playerOne = true;
// Handles SDL events
SDL_Event event;
// Array of Holders type
Holder holders[HOLDERS];
// Setup the holders
for (int a = 0, x = 20, y = 20; a < HOLDERS; a++, x+= 190)
{
if ( x > 400)
{
x = 20;
y += 190;
}
holders[a].xPos = x;
holders[a].yPos = y;
holders[a].width = 180;
holders[a].height = 180;
holders[a].isVacant = true;
}
/*---------------------------------------------------------------------------------------------------*/
/*--------------------------- MAIN LOOP -----------------------------------------------------*/
/*---------------------------------------------------------------------------------------------------*/
// Game loop
while (isRunning)
{
/*---------------------------------------------------------------------------------------------------*/
/*--------------------------- 1. EVENTS --------------------------------------------------*/
/*---------------------------------------------------------------------------------------------------*/
while (SDL_PollEvent(&event))
{
// If window is closed
if (event.type == SDL_QUIT)
{
isRunning = false;
}
// If escape key is pressed
if (event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_ESCAPE)
{
isRunning = false;
}
// Mouse click
if (event.type == SDL_MOUSEBUTTONDOWN)
{
if (event.button.button == SDL_BUTTON_LEFT)
{
// Mouse offsets
int x = 0, y = 0;
x = event.button.x;
y = event.button.y;
for (int a = 0; a < HOLDERS; a++)
{
if ((x > holders[a].xPos) && (x < holders[a].xPos + holders[a].width) &&
(y > holders[a].yPos) && (y < holders[a].yPos + holders[a].height) && holders[a].isVacant == true)
{
OutputDebugString("Spot no longer vacant\n");
holders[a].isVacant = false;
}
}
}
}
}
/*---------------------------------------------------------------------------------------------------*/
/*--------------------------- 2. LOGIC -----------------------------------------------------*/
/*---------------------------------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------------------------------*/
/*--------------------------- 3. RENDER -----------------------------------------------------*/
/*---------------------------------------------------------------------------------------------------*/
// Clears screen before rendering
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix(); // Start phase
glOrtho(0,600,600,0,-1,1); // Set the matrix
// THE HOLDERS
glBegin(GL_QUADS);
// Used to display vacant spaces black
for (int n = 0; n < HOLDERS; n++)
{
if (holders[n].isVacant == true)
{
glColor4ub(0,0,0,255);
glVertex2f(holders[n].xPos, holders[n].yPos);
glVertex2f(holders[n].xPos + holders[n].width, holders[n].yPos);
glVertex2f(holders[n].xPos + holders[n].width, holders[n].yPos + holders[n].height);
glVertex2f(holders[n].xPos, holders[n].yPos + holders[n].height);
}
else if (holders[n].isVacant == false && playerOne == true)
{
glColor4ub(0,0,255,255);
glVertex2f(holders[n].xPos, holders[n].yPos);
glVertex2f(holders[n].xPos + holders[n].width, holders[n].yPos);
glVertex2f(holders[n].xPos + holders[n].width, holders[n].yPos + holders[n].height);
glVertex2f(holders[n].xPos, holders[n].yPos + holders[n].height);
}
else if (holders[n].isVacant == false && playerOne == false)
{
glColor4ub(255,0,0,255);
glVertex2f(holders[n].xPos, holders[n].yPos);
glVertex2f(holders[n].xPos + holders[n].width, holders[n].yPos);
glVertex2f(holders[n].xPos + holders[n].width, holders[n].yPos + holders[n].height);
glVertex2f(holders[n].xPos, holders[n].yPos + holders[n].height);
}
}
glEnd();
glPopMatrix(); // End phase
// Draws everything above it to the screen
SDL_GL_SwapBuffers();
}
SDL_Quit;
return 0;
}[/source]