4 Replies - 28000 Views - Last Post: 18 March 2013 - 02:25 PM Rate Topic: -----

#1 absoluT88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-March 13

Question/help with Class Inheritance

Posted 18 March 2013 - 12:17 PM

I have created a basic game with SDL without using classes and have now been trying to backtrack and clean it up, doing it the correct way. As the title states, I have been having some trouble with class inheritance and receiving access violation errors. The access violation is coming when I try to blit a player img from Player class onto a screen surface from a Game class.

If anyone has any help it would be appreciated:

Player.h
#ifndef PLAYER_H
#define PLAYER_H

#include "Game.h"

class Player : public Game
{
public:

	void Init();
	void Clean();

	void HandleEvents(Player* player);
	void Update();
	void Draw();

private:

	SDL_Surface* s_player;

	SDL_Rect player[2];
};

#endif



Player.cpp
#include "Player.h"


void Player::Init()
{
	s_player = IMG_Load("player.bmp");
}

void Player::Clean()
{
	SDL_FreeSurface(s_player);
}
void Player::HandleEvents(Player* player)
{
	SDL_Event event;

}

void Player::Update()
{
	SDL_BlitSurface(s_player, NULL, s_screen , NULL);
}

void Player::Draw()
{

}



Game.h
#ifndef GAME_H
#define GAME_H

#include <SDL.h>
#include <SDL_image.h>

class Game
{
protected:
	
	SDL_Surface* s_screen;

public:

	void Init();
	void Clean();

	void HandleEvents(Game* game);
	void Update();
	void Draw();

	bool Running();
	void Quit();

private:

	bool isRunning;
};

#endif


Game.cpp
#include "Game.h"

void Game::Init()
{
	SDL_Init(SDL_INIT_EVERYTHING);

	s_screen = SDL_SetVideoMode(800,600,32,SDL_SWSURFACE);
}

void Game::Clean()
{

}

void Game::HandleEvents(Game* game)
{
	SDL_Event event;

	if (SDL_PollEvent(&event))
	{
		switch(event.type)
		{
		case SDL_QUIT:
			game->Quit();
			break;
		}

		switch(event.key.keysym.sym)
		{
		case SDLK_ESCAPE:
			game->Quit();
			break;
		}
	}


}

void Game::Update()
{
	SDL_BlitSurface(NULL, NULL, s_screen, NULL);
}

void Game::Draw()
{
	SDL_Flip(s_screen);
}

bool Game::Running()
{
	return isRunning;
}

void Game::Quit()
{
	isRunning = false;
}



Main.cpp
#include "Player.h"

int main(int argc, char* argv[])
{
	Game game;
	Player player;

	game.Init();
	player.Init();

	while (game.Running())
	{
		game.HandleEvents(&game);
		player.Update();
		game.Draw();
	}

	game.Clean();
	player.Clean();

	return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Question/help with Class Inheritance

#2 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 998
  • View blog
  • Posts: 4,173
  • Joined: 14-February 08

Re: Question/help with Class Inheritance

Posted 18 March 2013 - 01:07 PM

Could you post the full error please.

Why is Player inheriting from Game?
Was This Post Helpful? 0
  • +
  • -

#3 absoluT88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-March 13

Re: Question/help with Class Inheritance

Posted 18 March 2013 - 01:33 PM

Well I was just using this as a test. But I was having the player inherit the game so I could use the screen surface that the Game class has.

Posted Image

First-chance exception at 0x681247e3 in gameOne.exe: 0xC0000005: Access violation reading location 0xccccccf8.
Unhandled exception at 0x681247e3 in gameOne.exe: 0xC0000005: Access violation reading location 0xccccccf8.
The program '[2716] gameOne.exe: Native' has exited with code -1073741819 (0xc0000005).


Was This Post Helpful? 0
  • +
  • -

#4 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Question/help with Class Inheritance

Posted 18 March 2013 - 01:45 PM

Why is there no code in there to check if the operations are successful or not?!
Was This Post Helpful? 0
  • +
  • -

#5 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 998
  • View blog
  • Posts: 4,173
  • Joined: 14-February 08

Re: Question/help with Class Inheritance

Posted 18 March 2013 - 02:25 PM

And which s_screen variable do you think Player will use? Player uses s_screen which it inherits from Game but nowhere in Player does it actually initialise this variable. Do yourself a favour and drop the Player inheriting from Game and just pass in the variable from Game itself

Player.h
void Draw(SDL_Surface* pScreen);



Player.cpp
void Player::Draw(SDL_Surface* pScreen)
{
   SDL_BlitSurface(s_player, NULL, pScreen , NULL);
}



Game.h
SDL_Surface* getScreen() { return m_pScreen; }



main.cpp
#include "Player.h"

int main(int argc, char* argv[])
{
	Game game;
	Player player;

	game.Init();
	player.Init();

	while (game.Running())
	{
		game.HandleEvents(&game);
		player.Draw(game.getScreen());
		game.Draw();
	}

	game.Clean();
	player.Clean();

	return 0;
}



Or something like that.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1