Jump to content
  • Advertisement
Sign in to follow this  
Tynnhammar

[SDL] Blitting problem

This topic is 4847 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Greets all! I'm trying doing a Space Invader clone, however I've run into some class problems. The problem is that I have one class method, void CGameLoop::GameLoop(), where the main gameloop takes place, and I aswell blit's everything here. And that's the problem! Because now I'm trying to implement a CBullet class, and I can't seem to figure out how to blit it right? Please help! Here is the code: game.cpp
#include "sdl.h"
#include "game.h"

#include <fstream>
#include <iostream>


//Sets ups the basic things needed
CInit::CInit()
{
	m_HEIGHT = 600;
	m_WIDTH = 800;

	if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO) == -1)
	{
		std::cerr << SDL_GetError() << std::endl;
	}
	
	m_MainDisplay = SDL_SetVideoMode(m_WIDTH, m_HEIGHT, 0, SDL_FULLSCREEN|SDL_DOUBLEBUF);
	if(!m_MainDisplay)
	{
		std::cerr << SDL_GetError() << std::endl;
	}

	SDL_ShowCursor(0);
	SDL_WM_SetCaption("Space Attack!",0);
}

CInit::~CInit()
{
	SDL_Quit();
}
// The main game loop
void CGameLoop::GameLoop()
{
	m_Running = true;

	for(int k = 0; k < 323; ++k)
	{
		m_KeysHeld[k] = false;
	}

	m_Display = SDL_GetVideoSurface();

	m_ShipPic = SDL_LoadBMP("ship.bmp");
	
	m_SrcShip.x = 0;
	m_SrcShip.y = 0;
	m_SrcShip.h = 18;
	m_SrcShip.w = 36;

	m_DestShip.x = 760/2;
	m_DestShip.y = 570;
	m_DestShip.h = 18;
	m_DestShip.w = 36;

	SDL_BlitSurface(m_ShipPic, &m_SrcShip, m_Display, &m_DestShip);

	while(m_Running)
	{
		CGameLoop::CheckInput();

		CGameLoop::CheckWallCollision();

		SDL_FillRect(m_Display, NULL, SDL_MapRGB(m_Display->format, 0, 0, 0));
		SDL_BlitSurface(m_ShipPic, &m_SrcShip, m_Display, &m_DestShip);
		SDL_Flip(m_Display);
	}
}
// Checks for user input 
void CGameLoop::CheckInput()
{
	if(SDL_PollEvent(&Input))
	{
		if(Input.type == SDL_QUIT)
		{
			m_Running = false;
		}
		if(Input.type == SDL_KEYDOWN)
		{
			m_KeysHeld[Input.key.keysym.sym] = true;
		}
		if(Input.type == SDL_KEYUP)
		{
			m_KeysHeld[Input.key.keysym.sym] = false;
		}
	}
	
	if(m_KeysHeld[SDLK_ESCAPE])
	{
			m_Running = false;
	}
	if(m_KeysHeld[SDLK_LEFT])
	{
		CGameLoop::MoveLeft();
	}
	if(m_KeysHeld[SDLK_RIGHT])
	{	
		CGameLoop::MoveRight();
	}
	if(m_KeysHeld[SDLK_SPACE])
	{
		CGameLoop::Shoot();
	}
}

void CGameLoop::MoveLeft()
{
	m_DestShip.x -= 4;
}

void CGameLoop::MoveRight()
{
	m_DestShip.x += 4;
}

void CGameLoop::CheckWallCollision()
{
	if(m_DestShip.x > 760)
	{
		m_DestShip.x = 760;
	}
	
	if(m_DestShip.x < 20)
	{
		m_DestShip.x = 20;
	}
}

void CGameLoop::Shoot()
{
	// PSEUDO:    SKJUT, RÄKNA TILL 5
	
	CBullet::CBullet();

	SDL_Delay(500);
}

CBullet::CBullet()
{
	m_Display = SDL_GetVideoSurface();

	m_BulletPic = SDL_LoadBMP("bullet.bmp");

	m_SrcBullet.x = 0;
	m_SrcBullet.y = 0;
	m_SrcBullet.h = 18;
	m_SrcBullet.w = 36;

	m_DestBullet.x = 760/2;
	m_DestBullet.y = 570;
	m_DestBullet.h = 18;
	m_DestBullet.w = 36;

	SDL_BlitSurface(m_BulletPic, &m_SrcBullet, m_Display, &m_DestBullet);
}

CBullet::~CBullet()
{

}



game.h
#ifndef GAME_H
#define GAME_H

class CInit
{
public:
	CInit();
	~CInit();

	SDL_Surface* m_MainDisplay;

	int m_HEIGHT;
	int m_WIDTH;
};

class CGameLoop
{
public:
	void CheckInput();
	void GameLoop();

	SDL_Event Input;

	bool m_KeysHeld[323];
	bool m_Running;

private:
	void MoveLeft();
	void MoveRight();
	void CheckWallCollision();
	void Shoot();

	SDL_Surface* m_ShipPic;
	SDL_Surface* m_Display;
	SDL_Rect m_SrcShip;
	SDL_Rect m_DestShip;
};

class CBullet
{
public:
	CBullet();
	~CBullet();

	SDL_Surface* m_BulletPic;
	SDL_Surface* m_Display;
	SDL_Rect m_SrcBullet;
	SDL_Rect m_DestBullet;
};

#endif


main.cpp

#include "SDL.h"
#include "game.h"

int main(int argc, char* argv[])
{

	CInit Init;

	CGameLoop Game;

	Game.GameLoop();

	SDL_Quit();
	
	return 0;
}	

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!