4 Replies - 2329 Views - Last Post: 07 August 2010 - 09:05 AM Rate Topic: -----

#1 ReclusiveManiac  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 98
  • Joined: 18-September 09

Making games in Win32 API

Posted 06 August 2010 - 10:22 PM

Hey guys, sorry my title isn't very descriptive but I had a complicated question I wanted to ask you guys about.

So I'm making a game using the Win32 API and it's going really well, but the GamePaint() function I have which writes all the pixels to the screen is beginning to get cluttered with if statements. I will give you guys an example below of what I mean.


GamePaint()
{
    if (bPlayerTwoConnected == TRUE)
        pConnected->Draw();
    if (bLoadingScreen == TRUE)
        pLoadingScreen->Draw();
    if (bGameStarted == TRUE)
    {
        pBackground->Draw();
        pPlayerOne->Draw();
        pPlayerTwo->Draw();
    }
}

GameCycle()
{
//This is the main game cycle
}



As you guys can see, since my game doesn't have a whole lot of different bitmaps in it, I don't have to worry about drawing too many things. But if I wanted to make like a small 2d RPG-like game where you can move around and go to different towns/go in houses/fight monsters, I would have a lot of if statements similar to

if (bInHouse1)
    House1->Draw();
else if (bInHouse2)
    House2->Draw();
if (bInCity1)
    City1->Draw();



Hopefully you guys kind of see what I am asking because its kind of complicated. I'm basically just trying to figure out how to structure my GamePaint() function without using if statements to test every possible scenario. That just doesn't seem very Object Oriented to me. Thanks to anyone that can read this and help me out, I would really appreciate it.

Is This A Good Question/Topic? 0
  • +

Replies To: Making games in Win32 API

#2 Eclipse Reborn  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 135
  • Joined: 26-June 10

Re: Making games in Win32 API

Posted 07 August 2010 - 01:19 AM

Probably not the best solution, but you could use the conditional operator.

void GamePaint(/*Pass your arguments in here, don't use global*/)
{
	bPlayerTwoConnceted ? pConnected->Draw() : /*false cond*/;
	bLoadingScreen ? pLoadingScreen->Draw : /*false cond*/;
	bGameStarted ? pBackground->Draw(), pPlayerOne->Draw(), pPlayerTwo->Draw() : /*false cond*/;
}


And why are you using global variables? You should pass them in as arguments to functions.
Was This Post Helpful? 0
  • +
  • -

#3 ReclusiveManiac  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 98
  • Joined: 18-September 09

Re: Making games in Win32 API

Posted 07 August 2010 - 01:50 AM

Thanks for the reply. What do you mean by global variables? I do like the use of conditional operators more than what I was doing.

Edit: I know what global variables are, I just don't understand where you see me using them.

Edit2: Nevermind I was being blind. I see the global variable now. Some of them were, but some of them were also function calls to the PlayerOne and PlayerTwo.

This post has been edited by ReclusiveManiac: 07 August 2010 - 02:20 AM

Was This Post Helpful? 0
  • +
  • -

#4 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Making games in Win32 API

Posted 07 August 2010 - 08:45 AM

The correct term for the ? : syntax is "ternary" operator.

That's probably the easiest way to do it but there is still a lot of code. Maybe if you could design some sort of table with function pointers and a boolean value, you could loop through and do it in a few lines of code. But, if that's beyond your knowledge then the ternary operator is the way to go.

I don't know much about function pointers so I can't whip up a quick example.

Here is a tutorial/reference on function pointers:
http://www.newty.de/fpt/index.html
http://www.cprogramm...n-pointers.html

This post has been edited by eker676: 07 August 2010 - 08:46 AM

Was This Post Helpful? 1
  • +
  • -

#5 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Making games in Win32 API

Posted 07 August 2010 - 09:05 AM

The problem stems from bad design and hardcoding of data. To understand what is wrong, consider: House1->Draw(); and House2->Draw(); Are there fundamentally two different behaviors involved here? How is drawing one kind of house different from another? Before you answer, think of adding two numbers. Like 5 and 6, and 3 and 1. Fundamentally, it's just arithmetic. What changes are the parameters. So in regards to houses, is there fundamentally different drawing behavior?

Which in turn suggests that you hardcoded data in your design. Rather than generically loading and handling data, you have to handcraft behavior for each particular piece of content.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1