• 15
• 15
• 11
• 9
• 10

screen undeclared?(SDL Problem. Should be very simle to fix.)

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

Recommended Posts

Hey, I am hoping to finish my Pong Clone that I started yesterday sometime today. Anyway, I am drawing the Ball on the screen now, and I get an error that says..... 28 C:\Documents and Settings\Chad\Desktop\Paddle_Ball\Pong_Clone.cpp screen' undeclared (first use this function) So I looked at a tutorial, to see where I needed to declare it, but they have it the same way I do.(Except I made a function to do it. To keep main as clean as possible.) Anyway, since I am making a function, I think I should have it declared in the function somewhere but I am not sure where. So I am posting my code here, and I will post ALL my errors that I get, so you can see what is going on. I have tried to figure this out for about 20 minutes, but nothing. Maybe I am just REALLY stupid!? Here is the code:
#include <SDL/SDL.h>

const int WINDOW_WIDTH=800;
const int WINDOW_HEIGHT=600;

#define FPS =30
#define FPS_RATE =1000/FPS

void Draw_Ball()
{

SDL_Rect source;
source.x=24;
source.x=27;
source.w=24;
source.h=27;

SDL_Rect dest;
dest.x=200;
dest.y=200;
dest.w=50;
dest.h=50;

SDL_BlitSurface(bitmap, &source, screen, &dest);
SDL_Flip(screen);
SDL_FreeSurface(bitmap);
}

void Init()
{

SDL_Init( SDL_INIT_VIDEO );

SDL_Surface* screen = SDL_SetVideoMode( WINDOW_WIDTH, WINDOW_HEIGHT, 0,
SDL_HWSURFACE | SDL_DOUBLEBUF );
SDL_WM_SetCaption( WINDOW_TITLE, 0 );

SDL_Event event;
bool gameRunning = true;

while (gameRunning)
{
if (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
gameRunning = false;
}
}

}

}

int main(int argc, char **argv)
{
Init();
Draw_Ball();
SDL_Quit();
return 0;
}

ERRORS:
C:\Documents and Settings\Chad\Desktop\Paddle_Ball\Pong_Clone.cpp In function void Draw_Ball()': //telling me it is in that function

28 C:\Documents and Settings\Chad\Desktop\Paddle_Ball\Pong_Clone.cpp screen' undeclared (first use this function) //not sure on this one.

(Each undeclared identifier is reported only once for each function it appears in.)

C:\Dev-Cpp\Paddle_Ball\Makefile.win [Build Error]  ["../../Documents and Settings/Chad/Desktop/Paddle_Ball/Pong_Clone.o"] Error 1 /*I think it is because I haven't compiled it yet.  I think it will compile without a build error when I fix the error.  Not sure though.*/

`
Their were more errors, but I fixed those. NOTE:I AM JUST DRAWING THE BALL SOMEWHERE RIGHT NOW! I WILL FIGURE OUT HOW TO DRAW IT IN THE CENTER LATER. What is wrong? Chad!

Share on other sites
Your Draw_Ball() function doesn't have access to the screen variable, which you declared as a local variable to Init(). Either make screen a global variable, or pass the screen as an argument to the Draw_Ball() funciton.

Share on other sites
or use SDL_GetVideoSurface()

no nasty global variables / unnessecary arguments!

Share on other sites
Oh duh! I should have known that! Ok I will pass it as an argument. Oh yeah, what should the varable type of screen be? I tried int, but it said illigal conversation, so I am not sure. Could you please show me it via code, or something? PLEASE? LOL.

Thanks,

Share on other sites
just change:
SDL_BlitSurface(bitmap, &source, screen, &dest);
SDL_Flip(screen);
to:
SDL_BlitSurface(bitmap, &source, SDL_GetVideoSurface(), &dest);
SDL_Flip(SDL_GetVideoSurface());

I am not very sure about the performance difference, as I have not used this function very much.

Share on other sites
Thanks! That helped!

Now I got to figure out why my image isn't being displayed. If I can't figure out why, then I might post the problem here.

Share on other sites
It's because of the fact that you are drawing the image AFTER your message loop.
Put DrawBall inside the message loop, and it will work.

Also, clean up DrawBall, take the image loading stuff out, and put it elsewhere.

Share on other sites
lol, thanks for that. To bad I FINNALY figured that out. lol. Thanks for trying though.

What you mean by.."Also, clean up DrawBall, take the image loading stuff out, and put it elsewhere." Where else should I put it?