data structures and graphics
Just a quick question i suppose but how do you link the data structure you have to the graphics.
What i mean is i know how to draw lines and basic shapes using gdi in win32 programming and also that the board in a game of tic tac toe could be represented using a 2d array but how do these two items relate?
How about them apples?
How about them apples?
I''m not sure if I understand what you''re asking, but I''ll try to tackle that, popcorn.
For the example of tic-tac-toe, you might have a data structure, particularly a class, which represents one square.
It might have data such as what that square contains (X, O, or nothing) and where it is located in the drawing area, as well as a function which draws it.
Then a two-dimensional array could be used whose elements are Square objects. Then, to use it, you would, for instance, set a square''s value saying squares[row][column].contains(X). Then you could draw using something like squares[row][column].draw().
That''s almost certainly not the best way to do things, and maybe that''s not what you''re getting at, but perhaps you get the idea: a class where its objects represent renderable things, with all neccessary data and functions built-in.
Am I close?
For the example of tic-tac-toe, you might have a data structure, particularly a class, which represents one square.
It might have data such as what that square contains (X, O, or nothing) and where it is located in the drawing area, as well as a function which draws it.
Then a two-dimensional array could be used whose elements are Square objects. Then, to use it, you would, for instance, set a square''s value saying squares[row][column].contains(X). Then you could draw using something like squares[row][column].draw().
That''s almost certainly not the best way to do things, and maybe that''s not what you''re getting at, but perhaps you get the idea: a class where its objects represent renderable things, with all neccessary data and functions built-in.
Am I close?
I wanna play too!
Here's the psuedo-code that I'm picturing.
So what we've done here is incapsulated the entire data structure inside a class, giving the responsibility of using that data structure's contents to the classes methods.
So we made class methods to take that data and convert it into commands to the graphics api. This way, when you go to use that class, you don't have to care about how the api is going to draw to the screen, because it's implementation is irrelevent. All you need to know is how to use the classes methods. Technically, it would be better to make the data structure private and provide methods to change it's data, but I'm lazy. Don't be like me.
Anyway, some folks call that Abstraction. Learn to use it cause it'll save you time and effort, especially were graphical api and/or data structures are concerned.
-Infin.
[edited by - Infin on January 15, 2003 2:08:42 AM]
Here's the psuedo-code that I'm picturing.
Class CheckerBoard://public because game code needs to move peices around the boardpublic char boardArray[HEIGHT][WIDTH];//method to take boardArray and render it to the screenpublic void DrawBoard(){ for( int y = 0; y < HEIGHT; y++ ) { for( int x = 0; x < WIDTH; x++ ) { DrawSquare(x, y); } }}//private void DrawSquare( int x, int y ){ //some api code here to draw a sqaure ... if( boardArray[x][y] == 'R' ) draw red piece else if ( boardArray[x][y] == 'B' ) draw black piece else square is blank}
So what we've done here is incapsulated the entire data structure inside a class, giving the responsibility of using that data structure's contents to the classes methods.
So we made class methods to take that data and convert it into commands to the graphics api. This way, when you go to use that class, you don't have to care about how the api is going to draw to the screen, because it's implementation is irrelevent. All you need to know is how to use the classes methods. Technically, it would be better to make the data structure private and provide methods to change it's data, but I'm lazy. Don't be like me.
Anyway, some folks call that Abstraction. Learn to use it cause it'll save you time and effort, especially were graphical api and/or data structures are concerned.
-Infin.
[edited by - Infin on January 15, 2003 2:08:42 AM]
Thanks for the replies guys, i am sure that what i am doing is totally the wrong way to think about this problem but what i meant with the gdi and win32 stuff was that i was going to use the gdi functions such as the line function to draw a board on the screen. with the line function i would just draw two horizontal lines onto the window with a space between it and then two vertical lines with a space between it which would obviously intersect the two horizontal lines. this would create the board which the user sees on their window. Like i said the actual data structure which i am sure would represent the board in code form is obviously a 2d array. but how would these two things acutally be connected. I am sure that the ideas that you guys have come up with are probably more the correct way of going about things.
At the moment i just cannot make the connection between the graphics on the screen and the data structure behind it. anymore help would be greatly appreciated.
How about them apples?
[edited by - popcorn on January 14, 2003 10:10:55 PM]
At the moment i just cannot make the connection between the graphics on the screen and the data structure behind it. anymore help would be greatly appreciated.
How about them apples?
[edited by - popcorn on January 14, 2003 10:10:55 PM]
How about them apples?
January 14, 2003 10:03 PM
First, don''t think about the screen at all. Think about the game as if you were blindfolded and you only know where things are as coordinates and tokens, like letters. Like for tic tac toe, you know you have a three by three grid with either nothing(we''ll use ''p''), an ''x'', or an ''o''.
So at first you have
Then you take the blindfold off and you are simply only going to print what is in the data structure on the screen, regardless of what the user is doing at that moment. So, say you start with a blank screen. You draw the board with nothing on it and then you look at the data structure. There''s nothing in the first element, so you don''t print anything Then you check the second, also nothing. Finally you get to the center and it has an x. So you call code to print an x in the center of the screen.
Finally, there is input from the user, probably mouse input. An event is sent, you process the message and find that the user clicked somewhere on the screen. You check to see if it was in the game area: yup. Then you check to see where in the game area it was and find it was a click on the lower right corner. This is probably all ifs and switchs, like
PSEUDOCODE: IF mouseclickX was between 300 and 350 AND mouseclickY was between 300 and 350 THEN the user clicked on the bottom right square and therefore you should send the tic-tac-toe data structure info to set this element.
And so on. The tic-tac-toe data structure should check to see who went last and then see if an element is already taken. Ie, if there''s already an X at the bottom right, don''t set the element again and don''t change the current user''s turn.
In summury: Games contain data(and logic that can be performed on it), the part that looks at the data and thus puts it on screen, and the part that reads user input and sends it to the data. The data will then look at the input and decide if it was worthy and update itself whereby the graphics will be put on the screen, etc... Data/logic, graphics, user input.
So at first you have
0120ppp1ppp2ppp
Some code comes and changes the center to an x 0120ppp1ppp2ppp
This section of the code only knows what''s inside this array and does logic, like checking for a winner. Entirely abstract from what is drawn on the screen.Then you take the blindfold off and you are simply only going to print what is in the data structure on the screen, regardless of what the user is doing at that moment. So, say you start with a blank screen. You draw the board with nothing on it and then you look at the data structure. There''s nothing in the first element, so you don''t print anything Then you check the second, also nothing. Finally you get to the center and it has an x. So you call code to print an x in the center of the screen.
Finally, there is input from the user, probably mouse input. An event is sent, you process the message and find that the user clicked somewhere on the screen. You check to see if it was in the game area: yup. Then you check to see where in the game area it was and find it was a click on the lower right corner. This is probably all ifs and switchs, like
PSEUDOCODE: IF mouseclickX was between 300 and 350 AND mouseclickY was between 300 and 350 THEN the user clicked on the bottom right square and therefore you should send the tic-tac-toe data structure info to set this element.
And so on. The tic-tac-toe data structure should check to see who went last and then see if an element is already taken. Ie, if there''s already an X at the bottom right, don''t set the element again and don''t change the current user''s turn.
In summury: Games contain data(and logic that can be performed on it), the part that looks at the data and thus puts it on screen, and the part that reads user input and sends it to the data. The data will then look at the input and decide if it was worthy and update itself whereby the graphics will be put on the screen, etc... Data/logic, graphics, user input.
thanks ap thats exactly the type of advice i was looking for.
does anyone know what has happened to this thread? In my computer anyway there seems to be an excessive amount of horizontal scrolling required to read a post.
How about them apples?
does anyone know what has happened to this thread? In my computer anyway there seems to be an excessive amount of horizontal scrolling required to read a post.
How about them apples?
How about them apples?
quote: Original post by popcorn
does anyone know what has happened to this thread? In my computer anyway there seems to be an excessive amount of horizontal scrolling required to read a post.
Infin broke it with his preformated text.
[edited by - rotos on January 14, 2003 11:30:10 PM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement