#include <iostream>
#include <cstdlib>
#include <SDL.h>
SDL_Surface* image;
SDL_Surface* screen;
int x = 0;
int y = 0;
void Init_images()
{
image = SDL_LoadBMP("LOL.bmp");
}
void DrawImg(SDL_Surface* theimg,int x,int y)
{
SDL_Rect rect;
rect.x = x;
rect.y = y;
SDL_BlitSurface(theimg,NULL,screen,&rect);
}
void DG()
{
DrawImg(screen,0,0);
}
void Drawscene()
{
DrawImg(image,x-1,y-2);
SDL_Flip(image);
}
int main(int argc,char * argv[])
{
if(SDL_Init(SDL_INIT_EVERYTHING) <0)
{
std::cout <<"error",SDL_GetError();
}
atexit(SDL_Quit);
screen = SDL_SetVideoMode(640,480,32,SDL_HWSURFACE|SDL_DOUBLEBUF);
if (screen == NULL)
{
std::cout <<"error with setting it up", SDL_GetError();
}
Init_images();
DG();
int so = 0;
while (so == 0)
{
SDL_Event event;
while (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT) { so = 1; }
if (event.type == SDL_KEYDOWN)
{
if(event.key.keysym.sym == SDLK_ESCAPE ) {so = 1;}
}
}
}
Drawscene();
return 0;
}
SDL trouble
what is wrong? i compile it with no errors but it just shows a blank window
Try putting the call to DrawScene inside of your mainloop
// *** SNIP int so = 0; while (so == 0) { SDL_Event event; while (SDL_PollEvent(&event)) { if (event.type == SDL_QUIT) { so = 1; } if (event.type == SDL_KEYDOWN) { if(event.key.keysym.sym == SDLK_ESCAPE ) {so = 1;} } } // DrawScene after events processed Drawscene(); }
hmm, it gives me some errors now let me post my code to make sure i am doing it right
errors:
C:\Dev-Cpp\Untitled1w.cpp
[Warning] In function `int SDL_main(int, char**)':
62 C:\Dev-Cpp\Untitled1w.cpp
parse error before `{' token
Untitled1w.cpp C:\Dev-Cpp\Untitled1w.cpp
At global scope:
66 C:\Dev-Cpp\Untitled1w.cpp
ISO C++ forbids declaration of `Drawscene' with no type
67 C:\Dev-Cpp\Untitled1w.cpp
parse error before `return'
C:\Dev-Cpp\Makefile.win
[Build Error] [Untitled1w.o] Error 1
#include <iostream>#include <cstdlib>#include <SDL.h>SDL_Surface* image;SDL_Surface* screen;int x = 0;int y = 0;void Init_images(){ image = SDL_LoadBMP("LOL.bmp");}void DrawImg(SDL_Surface* theimg,int x,int y){ SDL_Rect rect; rect.x = x; rect.y = y; SDL_BlitSurface(theimg,NULL,screen,&rect);}void DG(){ DrawImg(screen,0,0);}int main(int argc,char * argv[]){ if(SDL_Init(SDL_INIT_EVERYTHING) <0) { std::cout <<"error",SDL_GetError(); } atexit(SDL_Quit); screen = SDL_SetVideoMode(640,480,32,SDL_HWSURFACE|SDL_DOUBLEBUF); if (screen == NULL) { std::cout <<"error with setting it up", SDL_GetError(); } Init_images(); DG(); int so = 0; while (so == 0) { SDL_Event event; while (SDL_PollEvent(&event)) { if (event.type == SDL_QUIT) { so = 1; } if (event.type == SDL_KEYDOWN) { if(event.key.keysym.sym == SDLK_ESCAPE ) {so = 1;} } } }void Drawscene(){ DrawImg(image,x-1,y-2); SDL_Flip(image);} Drawscene(); return 0;}
errors:
C:\Dev-Cpp\Untitled1w.cpp
[Warning] In function `int SDL_main(int, char**)':
62 C:\Dev-Cpp\Untitled1w.cpp
parse error before `{' token
Untitled1w.cpp C:\Dev-Cpp\Untitled1w.cpp
At global scope:
66 C:\Dev-Cpp\Untitled1w.cpp
ISO C++ forbids declaration of `Drawscene' with no type
67 C:\Dev-Cpp\Untitled1w.cpp
parse error before `return'
C:\Dev-Cpp\Makefile.win
[Build Error] [Untitled1w.o] Error 1
That's not what he meant hothead.
He meant move the Drawscene call in ur original program to within the outer While loop (just move it up the brace).
He meant move the Drawscene call in ur original program to within the outer While loop (just move it up the brace).
Quote:Original post by Specchum
That's not what he meant hothead.
He meant move the Drawscene call in ur original program to within the outer While loop (just move it up the brace).
it has the same errors ,but just to make sure is this what you meant?
#include <iostream>#include <cstdlib>#include <SDL.h>SDL_Surface* image;SDL_Surface* screen;int x = 0;int y = 0;void Init_images(){ image = SDL_LoadBMP("LOL.bmp");}void DrawImg(SDL_Surface* theimg,int x,int y){ SDL_Rect rect; rect.x = x; rect.y = y; SDL_BlitSurface(theimg,NULL,screen,&rect);}void DG(){ DrawImg(screen,0,0);}int main(int argc,char * argv[]){ if(SDL_Init(SDL_INIT_EVERYTHING) <0) { std::cout <<"error",SDL_GetError(); } atexit(SDL_Quit); screen = SDL_SetVideoMode(640,480,32,SDL_HWSURFACE|SDL_DOUBLEBUF); if (screen == NULL) { std::cout <<"error with setting it up", SDL_GetError(); } Init_images(); DG(); void Drawscene(){ DrawImg(image,x-1,y-2); SDL_Flip(image);} int so = 0; while (so == 0) { SDL_Event event; while (SDL_PollEvent(&event)) { if (event.type == SDL_QUIT) { so = 1; } if (event.type == SDL_KEYDOWN) { if(event.key.keysym.sym == SDLK_ESCAPE ) {so = 1;} } } } Drawscene(); return 0;}
#include <iostream>#include <cstdlib>#include <SDL.h>SDL_Surface* image;SDL_Surface* screen;int x = 0;int y = 0;void Init_images(){ image = SDL_LoadBMP("LOL.bmp");}void DrawImg(SDL_Surface* theimg,int x,int y){ SDL_Rect rect; rect.x = x; rect.y = y; SDL_BlitSurface(theimg,NULL,screen,&rect);}void DG(){ DrawImg(screen,0,0);}void Drawscene(){ DrawImg(image,x-1,y-2); SDL_Flip(image);}int main(int argc,char * argv[]){ if(SDL_Init(SDL_INIT_EVERYTHING) <0) { std::cout <<"error",SDL_GetError(); } atexit(SDL_Quit); screen = SDL_SetVideoMode(640,480,32,SDL_HWSURFACE|SDL_DOUBLEBUF); if (screen == NULL) { std::cout <<"error with setting it up", SDL_GetError(); } Init_images(); DG(); int so = 0; while (so == 0) { SDL_Event event; while (SDL_PollEvent(&event)) { if (event.type == SDL_QUIT) { so = 1; } if (event.type == SDL_KEYDOWN) { if(event.key.keysym.sym == SDLK_ESCAPE ) {so = 1;} } } // Call Drawscene here Drawscene(); } return 0;}
Leave all the functions implementations where they are - just call Drawscene in your inner loop.
Where you had:
int so = 0; while (so == 0) { SDL_Event event; while (SDL_PollEvent(&event)) { if (event.type == SDL_QUIT) { so = 1; } if (event.type == SDL_KEYDOWN) { if(event.key.keysym.sym == SDLK_ESCAPE ) {so = 1;} } } } Drawscene(); // Move this call from here to the inner loop return 0;
Change it to:
int so = 0; while (so == 0) { SDL_Event event; while (SDL_PollEvent(&event)) { if (event.type == SDL_QUIT) { so = 1; } if (event.type == SDL_KEYDOWN) { if(event.key.keysym.sym == SDLK_ESCAPE ) {so = 1;} } } // Change here Drawscene(); } return 0;
What's happening is that your call to draw the image on the screen (Drawscene) is only being called after your eventloop (the section that Polls the events looking for the escape key which sets the
so
flag variable). You need to move this to within the main loop so that the screen in drawn after the events have been processed but the escape key hasn't been hit.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement