Im A Failure With Header Files

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
Skinde
Posts: 1
Joined: September 30th, 2017, 1:29 am

Im A Failure With Header Files

Post by Skinde » September 30th, 2017, 1:41 am

I tried coding a program in which a box called car moves around a track with a green "zone" by minute 5 I had already failed, the car doesn't draw and I can't figure out the getters and setters, plz help...

The Header File:

Code: Select all

#pragma once

#include "Keyboard.h"
#include "Mouse.h"
#include "Graphics.h"


class Game
{
public:
	Game( class MainWindow& wnd );
	Game( const Game& ) = delete;
	Game& operator=( const Game& ) = delete;
	void Go();
private:
	void ComposeFrame(int x = 1, int y = 1, int r = 0, int g = 0, int b = 0);
	void UpdateModel();
	/********************************/
	/*  User Functions              */
	/********************************/
private:
	MainWindow& wnd;
	Graphics gfx;
	/********************************/
	/*  User Variables              */
	int Carx = 100, Cary = 100; //Car X And Y Starting Coordinates
public:
	int Game::GetCarx(int x) //X Coordinate Getter
	{
		x = Carx;
		return x;
	}
	int Game::GetCary(int y) // Y Coordinate Getter 
	{
		y = Cary;
		return y;
	}
	/********************************/
};
The Cpp File:

Code: Select all

Game::Game( MainWindow& wnd )
	:
	wnd( wnd ),
	gfx( wnd )
{
}

void Game::Go()
{
	gfx.BeginFrame();	
	UpdateModel();
	ComposeFrame();
	gfx.EndFrame();
}

void Game::UpdateModel()
{
}

void Game::ComposeFrame(int x, int y, int r, int g, int b)
{
	int screen_height, screen_lenght, DrawX, DrawY; 
	GetCarx(DrawX); //Gets Car X and Y Coordinates Without Modisfyng them
	GetCary(DrawY);

	for (int DrawX; DrawX <= 200; DrawX++) //Suposed To Draw A Red Box
	{
		for (int DrawY; DrawY <= 300; DrawY++)
		{
			gfx.PutPixel(DrawX, DrawY, 200, 0, 0);
		}
	}


	for (int screen_height = 0; screen_height <= 799; screen_height++) //Draws The Green layer on the bottom of the screen
	{
		for (int screen_lenght = 400; screen_lenght <= 599; screen_lenght++)
		{
			gfx.PutPixel(screen_height, screen_lenght, 0, 200, 0);
		}
	}
	
}
Edited by albinopapa put code in code tags

Code: Select all

[code]
[/code]

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Im A Failure With Header Files

Post by albinopapa » September 30th, 2017, 5:56 am

First, I'm a little confused by this

Code: Select all

   int Game::GetCarx(int x) //X Coordinate Getter
   {
      x = Carx;
      return x;
   }
   int Game::GetCary(int y) // Y Coordinate Getter 
   {
      y = Cary;
      return y;
   }
You pass in an X and Y, then assign a member CarX and CarY to them and pass out the X and Y, why not just pass out the CarX and CarY?

Code: Select all

   int Game::GetCarx() //X Coordinate Getter
   {
      return Carx;
   }
   int Game::GetCary() // Y Coordinate Getter 
   {
      return Cary;
   }
The next thing kind of explains the first thing I suppose

Code: Select all

   int screen_height, screen_lenght, DrawX, DrawY; 
   GetCarx(DrawX); //Gets Car X and Y Coordinates Without Modisfyng them
   GetCary(DrawY);
Your Get functions return a value, so you can treat them like values themselves.

Code: Select all

   int screen_height, screen_lenght, DrawX, DrawY; 
   int DrawX = GetCarx(); // Gets car X coordinate without modifying them
   int DrawY = GetCary(); // Gets car Y coordinate without modifying them
Since the functions return a copy of CarX/CarY you can do anything you want with the returned values.

There is a way to get the style you are going for to work, but I believe having them return a value and assigning the returned value to a new variable as I have shown is a better route.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Im A Failure With Header Files

Post by albinopapa » September 30th, 2017, 5:58 am

Btw

Code: Select all

int screen_height, screen_lenght
Declaring your variables at the top of the function is a bad idea, you should declare them just before using them and you should initialize them when you declare them if possible. You have those two declared at the top of ComposeFrame, but then you declare new instances of them in the two for loops at the bottom.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Post Reply