Jump to content
  • Advertisement
Sign in to follow this  
RedRabbit

help with SDL!

This topic is 5456 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

Hi guys...ill post source and errors...i dont know what to ask. i think its because i call draw.h in both main.cpp and draw.cpp......please help! main.cpp
#include "draw.h"

int main(int argc, char **argv)
{
	Uint8* keys;

	if (SDL_Init(SDL_INIT_VIDEO) < 0)
	{
		printf("Error: %s", SDL_GetError());
		exit(1);
	}
	atexit(SDL_Quit);

	screen = SDL_SetVideoMode(800,600,32, SDL_HWSURFACE | SDL_DOUBLEBUF);

	if(screen == NULL)
	{
		printf("Error: %s", SDL_GetError());
		exit(1);
	}

	InitImages();
	DrawBG();

	int done=0;

	while (done == 0)
	{
		SDL_Event event;

		while (SDL_PollEvent(&event))
		{
			if(event.type == SDL_QUIT) { done = 1; }
			if(event.type == SDL_KEYDOWN)
			{
				if(event.key.keysym.sym == SDLK_ESCAPE) { done = 1; }
			}
		}
			
		keys = SDL_GetKeyState(NULL);
		if(keys[SDLK_UP]) { ypos -= 1; }

		DrawScene();
	}

	return 0;
}


draw.cpp
#include "draw.h"

int InitImages()
{
	image = SDL_LoadBMP("paddle.bmp");
	back  = SDL_LoadBMP("bg.bmp");
	return 0;
}

void DrawIMG(SDL_Surface *img, int x, int y)
{
	SDL_Rect dest;
	dest.x = x;
	dest.y = y;
	SDL_BlitSurface(img, NULL, screen, &dest);
}

void DrawIMG(SDL_Surface *img, int x, int y, int w, int h, int x2, int y2)
{
	SDL_Rect dest;
	SDL_Rect dest2;

	dest.x = x;
	dest.y = y;

	dest2.x  = x2;
	dest2.y  = y2;
	dest2.w  = w;
	dest2.h  = h;

	SDL_BlitSurface(img, &dest2, screen, &dest);
}

void DrawBG()
{
	DrawIMG(back, 0,0);
}

void DrawScene()
{
	DrawIMG(back, xpos-2, ypos-2, 132, 132, xpos-2, ypos-2);
	DrawIMG(image, xpos, ypos);

	SDL_Flip(screen);
}


draw.h
#ifndef _DRAW_H_
#define _DRAW_H_

#include <SDL/SDL.h>
#include <stdio.h>
#include <stdlib.h>

SDL_Surface *screen;
SDL_Surface *image;
SDL_Surface *back;

int xpos=0,ypos=0;

int InitImages();
int SDL_BlitSurface(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dest, SDL_Rect *destrect);
void DrawIMG(SDL_Surface *img, int x, int y);
void DrawIMG(SDL_Surface *img, int x, int y, int w, int h, int x2, int y2);
void DrawBG();
void DrawScene();

#endif


EDIT: (forgot errors lol) errors:
--------------------Configuration: SDL_Pong - Win32 Debug--------------------
Compiling...
main.cpp
Linking...
draw.obj : error LNK2005: "int ypos" (?ypos@@3HA) already defined in main.obj
draw.obj : error LNK2005: "int xpos" (?xpos@@3HA) already defined in main.obj
draw.obj : error LNK2005: "struct SDL_Surface * back" (?back@@3PAUSDL_Surface@@A) already defined in main.obj
draw.obj : error LNK2005: "struct SDL_Surface * screen" (?screen@@3PAUSDL_Surface@@A) already defined in main.obj
draw.obj : error LNK2005: "struct SDL_Surface * image" (?image@@3PAUSDL_Surface@@A) already defined in main.obj
Debug/SDL_Pong.exe : fatal error LNK1169: one or more multiply defined symbols found
Error executing link.exe.

SDL_Pong.exe - 6 error(s), 0 warning(s)

thanks :)

Share this post


Link to post
Share on other sites
Advertisement
Don't put variables in header files. Copy that chunk to one of your cpp files, and stick 'extern' infront of the ones in the header.

Also use inclusion guards. Not a problem yet, but it could end up being one later.

Share this post


Link to post
Share on other sites
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!