Sign in to follow this  
MrWaffles

Pointing to an Array Element

Recommended Posts

I figured out that the below wouldn't work so I was wondering how would I go about point to an array's element so I could make my Button1 use the value so it could react according to that value, or if possible just make Button1 = Board[0][0]. Thanks in advance.
Button1 = *Board[0][0];

Share this post


Link to post
Share on other sites
You declare a pointer, as usual:

Type *name = NULL;

You take the address of some variable, as usual:

pointer = &variable;

Adding these together:

// Declared elsewhere...
<WhateverType> *Button1 = 0;

// Pointing at array element.
Button1 = &Board[0][0];

This is assuming the elements of "Board" are values, and not themselves pointers.

Share this post


Link to post
Share on other sites
Uhh... I screwed up big time trying to do what you told me. Hmm.. I guess I should explain to you why I need to know how to do this. I'm trying to make a Tic Tac Toe game using SDL. So face I've been able to display the board and check boxes according to who's turn it is, and I've also made it display who's turn it is.

|Compiler Error:
|=== TicTacToe, Debug ===|
40|error: expected constructor, destructor, or type conversion before '=' token|
||=== Build finished: 1 errors, 0 warnings ===|


Source Code:
//Player Turn
int PlayerTurn = 0;

///////////////////////////////

int v1,v2,v3,v4,v5,v6,v7,v8,v9;

int *Po1 = NULL;

Po1 = &Board[0][0]; //Line where compiler says there is a problem.

///////////////////////////////

//The event structure
SDL_Event event;

//The font
TTF_Font *font = NULL;

//The color of the font
SDL_Color textColor = { 19, 198, 245 };

class Button
{
private:
//The attributes of the button
SDL_Rect box;

//The part of the button sprite sheet that will be shown
SDL_Rect* clip;

public:
//Initialize the variables
Button( int x, int y, int w, int h );

//Handles events and set the button's sprite region
void handle_events();

//Shows the button on the screen
void show();
};

Share this post


Link to post
Share on other sites
The problem is that the line is an assignment statement. This is not allowed outside a function body. You could merge the assignment into the declaration, by initialising like so:

int * Po1 = &Board[0][0];


This depends on "Board" being declared also.

Is this a header file?

Share this post


Link to post
Share on other sites
You cannot "run code" outside of a function in C++, except to initialize a variable. Initialization is different from "assigning to something immediately after declaring it". It refers to assigning a value while declaring the variable. In your code, you initialize the variable to NULL, and then attempt to assign the real value. In addition to being illegal outside of functions, this is nonsensical: use the real value for the initialization.

Also, what are "int v1,v2,v3,v4,v5,v6,v7,v8,v9;" for? You clearly know about arrays; why not use one for these variables?

Share this post


Link to post
Share on other sites
Maybe it would be better if I just show you the whole source code so it'll be easier to point out things that could be holding me back.

Full Code:

//The headers
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_ttf.h"
#include <string>

//Screen attributes
const int SCREEN_WIDTH = 324;
const int SCREEN_HEIGHT = 386;
const int SCREEN_BPP = 32;

//The button states in the sprite sheet
const int CLIP_MOUSEOVER = 0;
const int CLIP_MOUSEOUT = 1;
const int CLIP_MOUSEDOWN = 2;

//The surfaces
SDL_Surface *grid = NULL;
SDL_Surface *screen = NULL;
SDL_Surface *message = NULL;
SDL_Surface *message2 = NULL;

//SDL grid_tiles Rect
SDL_Rect clips[3];

//Grid Array
int Board [3][3] = {
{ 0, 0, 0 },
{ 0, 0, 0 },
{ 0, 0, 0 }
};

//Player Turn
int PlayerTurn = 0;

//The event structure
SDL_Event event;

//The font
TTF_Font *font = NULL;

//The color of the font
SDL_Color textColor = { 19, 198, 245 };

class Button
{
private:
//The attributes of the button
SDL_Rect box;

//The part of the button sprite sheet that will be shown
SDL_Rect* clip;

public:
//Initialize the variables
Button( int x, int y, int w, int h );

//Handles events and set the button's sprite region
void handle_events();

//Shows the button on the screen
void show();
};

SDL_Surface *load_image( std::string filename )
{
//The image that's loaded
SDL_Surface* loadedImage = NULL;

//The optimized surface that will be used
SDL_Surface* optimizedImage = NULL;

//Load the image
loadedImage = IMG_Load( filename.c_str() );

//If the image loaded
if( loadedImage != NULL )
{
//Create an optimized surface
optimizedImage = SDL_DisplayFormat( loadedImage );

//Free the old surface
SDL_FreeSurface( loadedImage );

//If the surface was optimized
if( optimizedImage != NULL )
{
//Color key surface
SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF ) );
}
}

//Return the optimized surface
return optimizedImage;
}

void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL )
{
//Holds offsets
SDL_Rect offset;

//Get offsets
offset.x = x;
offset.y = y;

//Blit
SDL_BlitSurface( source, clip, destination, &offset );
}

void set_board()
{
clips[ CLIP_MOUSEOVER ].x = 0;
clips[ CLIP_MOUSEOVER ].y = 0;
clips[ CLIP_MOUSEOVER ].w = 108;
clips[ CLIP_MOUSEOVER ].h = 112;

clips[ CLIP_MOUSEOUT ].x = 108;
clips[ CLIP_MOUSEOUT ].y = 0;
clips[ CLIP_MOUSEOUT ].w = 108;
clips[ CLIP_MOUSEOUT ].h = 112;

clips[ CLIP_MOUSEDOWN ].x = 216;
clips[ CLIP_MOUSEDOWN ].y = 0;
clips[ CLIP_MOUSEDOWN ].w = 108;
clips[ CLIP_MOUSEDOWN ].h = 112;
}

bool init()
{
//Initialize all SDL subsystems
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
{
return false;
}

//Set up the screen
screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );

//If there was an error in setting up the screen
if( screen == NULL )
{
return false;
}

//Initialize SDL_ttf
if( TTF_Init() == -1 )
{
return false;
}

//Set the window caption
SDL_WM_SetCaption( "Tic Tac Toe", NULL );

//If everything initialized fine
return true;
}

bool load_files()
{
//Load the Tic Tac Toe grid
grid = load_image( "images/Grid2.png" );

//Open the font
font = TTF_OpenFont( "fonts/mangat.ttf", 32 );

//If there was a problem in loading the grid
if( grid == NULL )
{
return false;
}

//If there was an error in loading the font
if( font == NULL )
{
return false;
}

//If everything loaded fine
return true;
}

void clean_up()
{
//Free the surfaces
SDL_FreeSurface( grid );
SDL_FreeSurface( message );
SDL_FreeSurface( message2 );

//Close the font
TTF_CloseFont( font );

//Quit SDL_ttf
TTF_Quit();

//Quit SDL
SDL_Quit();
}

Button::Button( int x, int y, int w, int h )
{
//Set the button's attributes
box.x = x;
box.y = y;
box.w = w;
box.h = h;

//Set the default sprite
clip = &clips[ CLIP_MOUSEOVER ];
}

void Button::handle_events()
{
//The mouse offsets
int x = 0, y = 0;

//If a mouse button was pressed
if( event.type == SDL_MOUSEBUTTONDOWN )
{
//If the left mouse button was pressed
if( event.button.button == SDL_BUTTON_LEFT )
{
//Get the mouse offsets
x = event.button.x;
y = event.button.y;

//If the mouse is over the button
if( ( x > box.x ) && ( x < box.x + box.w ) && ( y > box.y ) && ( y < box.y + box.h ) )
{
if( clip == &clips[ CLIP_MOUSEOVER ] )
{
if( PlayerTurn == 0 )
{
//Set the button sprite
clip = &clips[ CLIP_MOUSEOUT ];
PlayerTurn++;
}
else
{
//Set the button sprite
clip = &clips[ CLIP_MOUSEDOWN ];
PlayerTurn--;
}
}
}
}
}
}

void Button::show()
{
//Show the button
apply_surface( box.x, box.y, grid, screen, clip );
}

int main( int argc, char* args[] )
{
//Quit flag
bool quit = false;

//Initialize
if( init() == false )
{
return 1;
}

//Load the files
if( load_files() == false )
{
return 1;
}

//Clip the sprite sheet
set_board();

//Make the button
Button Button1( 0, 0+50, 108, 112 );
Button Button2( 0, 112+50, 108, 112 );
Button Button3( 0, 224+50, 108, 112 );
Button Button4( 108, 0+50, 108, 112 );
Button Button5( 108, 112+50, 108, 112 );
Button Button6( 108, 224+50, 108, 112 );
Button Button7( 216, 0+50, 108, 112 );
Button Button8( 216, 112+50, 108, 112 );
Button Button9( 216, 224+50, 108, 112 );

message = TTF_RenderText_Solid( font, "Player 1's Turn", textColor );
message2 = TTF_RenderText_Solid( font, "Player 2's Turn", textColor );

//If there was an error in rendering the text
if( message == NULL )
{
return 1;
}

//If there was an error in rendering the text
if( message2 == NULL )
{
return 1;
}

//Update the screen
if( SDL_Flip( screen ) == -1 )
{
return 1;
}

//While the user hasn't quit
while( quit == false )
{
//If there's an event to handle
while( SDL_PollEvent( &event ) )
{
//Handle button events

Button1.handle_events();
Button2.handle_events();
Button3.handle_events();
Button4.handle_events();
Button5.handle_events();
Button6.handle_events();
Button7.handle_events();
Button8.handle_events();
Button9.handle_events();

//If the user has Xed out the window
if( event.type == SDL_QUIT )
{
//Quit the program
quit = true;
}
}

//Fill the screen white
SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) );

//Show the button
Button1.show();
Button2.show();
Button3.show();
Button4.show();
Button5.show();
Button6.show();
Button7.show();
Button8.show();
Button9.show();

if( PlayerTurn == 0 )
{
apply_surface( 22, 0, message, screen );
}
else
{
apply_surface( 22, 0, message2, screen );
}

//Update the screen
if( SDL_Flip( screen ) == -1 )
{
return 1;
}
}

//Clean up
clean_up();

return 0;
}



Share this post


Link to post
Share on other sites
Put the buttons into an array. Use arithmetic on the array index to calculate drawing positions. Similarly, you don't need to create the RECTs for the button sprites ahead of time; you can use arithmetic on the CLIP_ value to calculate the x and y position on the fly, and substitute in the width and height from the Button's 'box', so all you really need to remember is the button's state. (And why not use an enum for those values?) To handle_events(), you can just loop through the array.

PlayerTurn shouldn't change until the mouse is *released*, inside the same button that was initially pressed. You should keep track somewhere of which button was selected when the mouse was pressed, and only alter its state until the button is released again. When the mouse is released, if it's inside the original button, you can send "Player wants to move here" logic to whatever code handles the Board contents.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this