biblioteka Sdl

0

Witam, mam problem ze skompilowaniem kodu prostej gry na Sdl, może ktoś mnie naprowadzić na przyczynę tych błędów?
user image
Z góry dzięki.

1

Z tego widać że mylisz obiekty ze wskaźnikiem na obiekty oraz nie umiesz poprawnie dołączyć plików nagłówkowych.
Jak chcesz więcej informacji to zgłoś się na forum wróżbitów lub podaj kod.

0

paddle.h

 #include <SDL.h>
#ifndef PADDLE_H
#define PADDLE_H

class paddle{
	SDL_Rect box;
	SDL_Surface* image;
	int yvel;
	int point;
	public:
	paddle(SDL_Surface* img,int x,int y,int w,int h,int yVel);
	~paddle();
	SDL_Rect* getRect();
	void moveUp();
	void moveDown();
	void show();
	void incpoint();
	void setBack(int x,int y,int w,int h,int yVel);
	int getPoints();
};

#endif

paddle.cpp

#include "paddle.h"

paddle::paddle(SDL_Surface* img,int x,int y,int w,int h,int yVel)
{
	box.x=x;
	box.y=y;
	box.w=w;
	box.h=h;
	image=img;
	yvel=yVel;
	point=0;
}

paddle::~paddle()
{
	SDL_FreeSurface(image);
}

void paddle::moveUp()
{
	if(box.y>0)
		box.y-=yvel;
}

void paddle::moveDown()
{
	if(box.y+box.h<SDL_GetVideoSurface()->clip_rect.h)
		box.y+=yvel;
}

void paddle::show()
{
	SDL_BlitSurface(image,NULL,SDL_GetVideoSurface(),&box);
}

SDL_Rect* paddle::getRect()
{
	return &box;
}

void paddle::incpoint()
{
	point++;
}

void paddle::setBack(int x,int y,int w,int h,int yVel)
{
	box.x=x;
	box.y=y;
	box.w=w;
	box.h=h;
	yvel=yVel;
}

int paddle::getPoints()
{
	return point;
}

ball.h

#include <SDL.h>
#include <SDL_mixer.h>
#ifndef BALL_H
#define BALL_H

class ball{
	int xvel,yvel;
	SDL_Surface* image;
	SDL_Rect box;
	bool collision(SDL_Rect* rec1,SDL_Rect* rec2);
	public:
	ball(SDL_Surface* img,int x,int y,int w,int h,int xVel,int yVel);
	~ball();
	void show();
	void move(SDL_Rect* player1,SDL_Rect* player2,Mix_Chunk*);
	int isOut();
	void setBack(int x,int y,int w,int h,int xVel,int yVel);
};


#endif 

ball.cpp

 #include "ball.h"

ball::ball(SDL_Surface* img,int x,int y,int w,int h,int xVel,int yVel)
{
	image=img;
	box.x=x;
	box.y=y;
	box.w=w;
	box.h=h;
	xvel=xVel;
	yvel=yVel;
}

ball::~ball()
{
	SDL_FreeSurface(image);
}

void ball::show()
{
	SDL_BlitSurface(image,NULL,SDL_GetVideoSurface(),&box);
}

void ball::move(SDL_Rect* player1,SDL_Rect* player2,Mix_Chunk* effect)
{
	box.x+=xvel;
	box.y+=yvel;
	if(box.y<=0)
		yvel=-yvel;
	if(box.y+box.h>=SDL_GetVideoSurface()->clip_rect.h)
		yvel=-yvel;
	if(collision(&box,player1))
	{
		Mix_PlayChannel(-1,effect,0);
		if(box.x+3<player1->x+player1->w)
			yvel=-yvel;
		else
			xvel=-xvel;
	}
	if(collision(&box,player2))
	{
		Mix_PlayChannel(-1,effect,0);
		if(box.x+box.w-3 > player2->x)
			yvel=-yvel;
		else
			xvel=-xvel;

	}
}

bool ball::collision(SDL_Rect* rec1,SDL_Rect* rec2)
{
	if(rec1->y >= rec2->y + rec2->h)
		return 0;
	if(rec1->x >= rec2->x + rec2->w)
		return 0;
	if(rec1->y + rec1->h <= rec2->y)
		return 0;
	if(rec1->x + rec1->w <= rec2->x)
		return 0;
	return 1;
}


int ball::isOut()
{
	if(box.x<=0)
		return 1;
	if(box.x>=SDL_GetVideoSurface()->clip_rect.w)
		return 2;
	return 0;
}


void ball::setBack(int x,int y,int w,int h,int xVel,int yVel)
{
	box.x=x;
	box.y=y;
	box.w=w;
	box.h=h;
	xvel=xVel;
	yvel=yVel;
}

main.cpp

#include <SDL.h>
#include <iostream>
#include <SDL_ttf.h>
#include <SDL_mixer.h>
#include "ball.h"
#include "paddle.h"


SDL_Surface* load_image(const char* c,Uint32 colorkey=0)
{
	SDL_Surface* tmp=SDL_LoadBMP(c);
	if(colorkey!=0)
	{
		SDL_SetColorKey(tmp,SDL_SRCCOLORKEY,colorkey);
	}
	return tmp;
}

int main(int argc,char* argv[])
{
	SDL_Surface* screen,*icon;
	const int width=640;
	const int FPS=30;
	const int height=480;
	screen=SDL_SetVideoMode(width,height,32,SDL_SWSURFACE);
	icon=load_image("icon.bmp");
	SDL_WM_SetIcon(icon,NULL);
	SDL_WM_SetCaption("Pong game",NULL);
	TTF_Font* font;
	TTF_Init();
	Mix_OpenAudio(22050,MIX_DEFAULT_FORMAT,2,4096);
	Mix_Music* music;
	Mix_Chunk* effect;
	music=Mix_LoadMUS("Hackbeat.wav");
	effect=Mix_LoadWAV("bassdrum1.wav");
	Mix_PlayMusic(music,-1);
	font=TTF_OpenFont("air.ttf",20);
	SDL_Color color = {0,0,0};
	SDL_Event event;
	Uint32 start;
	bool running=true;
	bool arr[4] = {0,0,0,0};
	paddle player1(load_image("paddle.bmp"),0,225,10,50,3);
	paddle player2(load_image("paddle.bmp"),width-10,225,10,50,3);
	ball ball1(load_image("ball.bmp",SDL_MapRGB(screen->format,0x00,0xff,0xff)),320,240,20,20,3,3);
	while(running)
	{
		start=SDL_GetTicks();
		
		while(SDL_PollEvent(&event))
		{
			switch(event.type)
			{
				case SDL_QUIT:
					running=false;
					break;
				case SDL_KEYDOWN:
					switch(event.key.keysym.sym)
					{
						case SDLK_UP:
							arr[0]=1;
							break;
						case SDLK_DOWN:
							arr[1]=1;
							break;

						case SDLK_w:
							arr[2]=1;
							break;
						case SDLK_s:
							arr[3]=1;
							break;
					}
					break;
				case SDL_KEYUP:
					switch(event.key.keysym.sym)
					{
						case SDLK_UP:
							arr[0]=0;
							break;
						case SDLK_DOWN:
							arr[1]=0;
							break;

						case SDLK_w:
							arr[2]=0;
							break;
						case SDLK_s:
							arr[3]=0;
							break;
					}
					break;
			}
		}
		
		if(arr[0])
			player2.moveUp();
		else if(arr[1])
			player2.moveDown();
		if(arr[2])
			player1.moveUp();
		else if(arr[3])
			player1.moveDown();
		ball1.move(player1.getRect(),player2.getRect(),effect);
		switch(ball1.isOut())
		{
			case 1:
				player2.incpoint();
				player1.setBack(0,225,10,50,3);
				player2.setBack(width-10,225,10,50,3);
				ball1.setBack(320,240,20,20,3,3);
				break;
			case 2:
				player1.incpoint();
				player1.setBack(0,225,10,50,3);
				player2.setBack(width-10,225,10,50,3);
				ball1.setBack(320,240,20,20,3,3);
				break;
		}

	
		SDL_FillRect(screen,&screen->clip_rect,SDL_MapRGB(screen->format,0xff,0xff,0xff));
		player1.show();
		player2.show();
		ball1.show();

		char c[5];
		SDL_Rect tmp = {10,0};
		sprintf(c,"%d",player1.getPoints());
		SDL_Surface* text=TTF_RenderText_Solid(font,c,color);
		SDL_BlitSurface(text,NULL,screen,&tmp);

		tmp.x=width-40;
		sprintf(c,"%d",player2.getPoints());
		text=TTF_RenderText_Solid(font,c,color);
		SDL_BlitSurface(text,NULL,screen,&tmp);
		SDL_FreeSurface(text);

		SDL_Flip(screen);
		
		if(1000/FPS>(SDL_GetTicks()-start))
			SDL_Delay(1000/FPS-(SDL_GetTicks()-start));
	}
	
	SDL_FreeSurface(icon);
	Mix_FreeMusic(music);
	Mix_FreeChunk(effect);
	Mix_CloseAudio();
	TTF_CloseFont(font);
	TTF_Quit();
	SDL_Quit();
} 

1 użytkowników online, w tym zalogowanych: 0, gości: 1