Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Smacker_626

C++ and SDL

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

Well whats there to say everything works to the point when a key is pressed and it go's thru the if statement but just doesn't add 1 to xpos and ypos... The problems are in the fKeyPressed function about half way down Code: uhh ya that was a little much so click here to download the ziped file >> Click here to download (Winrar>>Get it free Here) (Winzip>>Get it free Here) oh and thanks in advance to... well who ever lol --------------------signature-------------------- Looking for usefull, interesting resources WELL DONT LOOK HERE>>http://www.dragonruins.com/ but its still fun to have a look around [edited by - Smacker_626 on August 27, 2002 3:00:54 PM]

Share this post


Link to post
Share on other sites
Advertisement
quote:
Original post by ElCrazon
Link doesnt work, try posting all you souce inside [ source ] tags.


Sorry

I just put it up though and the serv is gonna be up (or so i hope im paying for it)

--------------------signature--------------------

Looking for usefull, interesting resources

WELL DONT LOOK HERE>>http://www.dragonruins.com/

but its still fun to have a look around

Share this post


Link to post
Share on other sites
I was gonna do this but didnt know how but even though i got it on my site this is for all the lazy peeps out there

EDIT ---it took out alot of the returns so i would get the file from my site its more... readable---


    

#include <iostream.h>
#include <stdlib.h>
#include "SDL.h"

SDL_Surface *screen;
SDL_Surface *back;
SDL_Surface *button;
SDL_Event event;

char cIamBlank[1];
int mxpos = 0, mypos = 0, xpos = 0, ypos = 0, buttonPressed = 0;
Uint8 *keys;

void fMessagePump();
void fPrintMessages();
void fKeyPressed(int km);
void fCheckMousePos();
unsigned int fBlitImg(SDL_Surface *srcSurf, SDL_Surface *dstSurf, int w, int h, int x, int y, int x2, int y2);
unsigned int fBlitImg2(SDL_Surface *srcSurf, SDL_Surface *dstSurf, int w, int h, int x, int y, int x2, int y2);
void fLoadImages();
void fDrawScreen();

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

if ((SDL_Init(SDL_INIT_VIDEO)) != 0)
{

cout << "Could not Init Video!";

exit (0);

}
else
{

cout << "Init Video Complete!\n";

}

atexit(SDL_Quit);

SDL_WM_SetCaption("Button Test!", NULL);
SDL_WM_SetIcon(SDL_LoadBMP("C:\\Program Files\\Microsoft Visual Studio\\MyProjects\\SDL Button Test\\Debug\\Mainicon.bmp"), NULL);

cout << "Trying to create Surface: \"screen\" at 600x400x32 using Hardware with Double Buffering...\n";

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

if (screen == NULL)
{

cout << "Failed! Trying without Double Buffering...\n";

screen = SDL_SetVideoMode(600, 400, 32, SDL_HWSURFACE);

if (screen == NULL)
{

cout << "Failed! Trying Software Surface...\n";

screen = SDL_SetVideoMode(600, 400, 32, SDL_SWSURFACE);

if (screen == NULL)
{

cout << "Failed! shuting down all systems\n\n";

exit (0);

}

cout << "Surface created!\n";

}

cout << "Surface created!\n";

}

cout << "Surface created!\n";

fLoadImages();

fDrawScreen();

fMessagePump();

return 0;

}

void fMessagePump()
{

for ( ; ; )
{

if (SDL_PollEvent(&event))
{

switch (event.type)
{

case SDL_QUIT:

fPrintMessages();

break;

case SDL_KEYDOWN:

fKeyPressed(0);

break;

case SDL_MOUSEBUTTONDOWN:

buttonPressed = 1;

fKeyPressed(1);

break;

case SDL_MOUSEBUTTONUP:

buttonPressed = 0;

fKeyPressed(1);

break;

default:

break;

}

}

}

}

void fPrintMessages()
{

cout << "\nMessages Printed type a charactor and hit enter to quit\n\n";

cin >> cIamBlank;

cout << "\n\n";

exit (0);

}

void fKeyPressed(int km)
{

if (km == 0)
{

keys = SDL_GetKeyState(NULL);

if (keys[SDLK_UP] == 1)
{

ypos -= 1;
//fLoadImages();

fDrawScreen();

}
if (keys[SDLK_DOWN] == 1)
{

ypos += 1;
//fLoadImages();

fDrawScreen();

}
if (keys[SDLK_LEFT] == 1)
{

xpos -= 1;
//fLoadImages();

fDrawScreen();

}
if (keys[SDLK_RIGHT] == 1)
{

xpos += 1;
//fLoadImages();

fDrawScreen();

}
if (keys[SDLK_ESCAPE] == 1)
{

fPrintMessages();

}

}
else if (km == 1)
{

if(event.button.button == SDL_BUTTON_LEFT);
{

fCheckMousePos();

}

}

return;

}

void fCheckMousePos()
{

int tempx, tempy;

tempx = ((xpos) + (button->w));
tempy = ((ypos) + (button->h));

SDL_GetMouseState(&mxpos, &mypos);

mxpos = event.button.x;
mypos = event.button.y;

if (mxpos >= xpos && mxpos <= tempx && mypos >= ypos && mypos <= tempy && buttonPressed == 1)
{

fLoadImages();

fDrawScreen();

cout << "Button Pressed!\n";

}
else if (mxpos >= xpos && mxpos <= tempx && mypos >= ypos && mypos <= tempy && buttonPressed == 0)
{

fLoadImages();

fDrawScreen();

cout << "Button Released!\n";

}

return;

}

void fLoadImages()
{

back = SDL_LoadBMP("C:\\Program Files\\Microsoft Visual Studio\\MyProjects\\SDL Button Test\\Debug\\back.bmp");

if (buttonPressed == 1)
{
button = SDL_LoadBMP("C:\\Program Files\\Microsoft Visual Studio\\MyProjects\\SDL Button Test\\Debug\\buttonP.bmp");

}
else if (buttonPressed == 0)
{

button = SDL_LoadBMP("C:\\Program Files\\Microsoft Visual Studio\\MyProjects\\SDL Button Test\\Debug\\buttonR.bmp");

}
/*
if (back == NULL)
{

cout << "Could not load background image!\n";

}
else
{

cout << "Loaded background image!\n";

}
if (button == NULL)
{

cout << "Could not load button image!\n";

}
else
{

cout << "Loaded button image!\n";

}
*/

return;

}

unsigned int fBlitImg(SDL_Surface *srcSurf, SDL_Surface *dstSurf, int w, int h, int x, int y, int x2, int y2)
{

unsigned int tempval = 0;


SDL_Rect dstRect, srcRect;

dstRect.x = x2;
dstRect.y = y2;

srcRect.h = h;
srcRect.w = w;
srcRect.x = x;
srcRect.y = y;

xpos = x2;
ypos = y2;

tempval = SDL_BlitSurface(srcSurf, &srcRect, dstSurf, &dstRect);

//SDL_FreeSurface(srcSurf);


return tempval;

}

unsigned int fBlitImg2(SDL_Surface *srcSurf, SDL_Surface *dstSurf, int w, int h, int x, int y, int x2, int y2)
{

unsigned int tempval = 0;


SDL_Rect dstRect, srcRect;

dstRect.x = x2;
dstRect.y = y2;

srcRect.h = h;
srcRect.w = w;
srcRect.x = x;
srcRect.y = y;

tempval = SDL_BlitSurface(srcSurf, &srcRect, dstSurf, &dstRect);

//SDL_FreeSurface(srcSurf);


return tempval;

}

void fDrawScreen()
{

unsigned int tempback, tempbutton;

tempback = fBlitImg(back, screen, back->w, back->h, 0, 0, 0, 0);

if (xpos == 0 || ypos == 0)
{

tempbutton = fBlitImg(button, screen, button->w, button->h, 0, 0, (((screen->w) - 128) /2), (((screen->h) - 128) /2));

}
else if (xpos != 0 || ypos != 0)
{

tempbutton = fBlitImg2(button, screen, button->w, button->h, 0, 0, xpos, ypos);

}

if (tempback != 0)
{

cout << "Could not Blit background image onto Surface: \"screen\" from Surface: \"back\"!\n";

}

if (tempbutton != 0)
{

cout << "Could not Blit button image onto Surface: \"screen\" from Surface: \"button\"!\n";

}

SDL_UpdateRect(screen, 0, 0, 0, 0);

return;

}



--------------------signature--------------------

Looking for usefull, interesting resources

WELL DONT LOOK HERE>>http://www.dragonruins.com/

but its still fun to have a look around

[edited by - Smacker_626 on August 27, 2002 3:24:46 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by ElCrazon
Ok the link work now... i''ll read through it after i finish my lunch..


Sounds good to me *munch.munch.munch*

--------------------signature--------------------

Looking for usefull, interesting resources

WELL DONT LOOK HERE>>http://www.dragonruins.com/

but its still fun to have a look around

Share this post


Link to post
Share on other sites
Ok, it compiles fine. What exactyly is your problem? Also could I get the images it loads?

[edit] oh wait, theres a Segmentation Fault, could I get all the files for this project? [/edit]

[edited by - ElCrazon on August 27, 2002 4:03:41 PM]

Share this post


Link to post
Share on other sites
The problem was that it wouldnt add 1 to ypos and xpos it always compiled fine.

(BTW how did it compile fine on ur comp do u have the SDL includes and libs?)

oh and also i got it fixed, I forgot to change one of the funtion calls so it kept setting y and xpos back to there starting values

and incase u wanted to see the files i updated them on the serv and im putting the source in here (also the zip on my serv has everything but the resource file and the icon so when u load the project it will tell u it couldnt open all the windows just click ok and go on with life)


    

#include <iostream.h>
#include <stdlib.h>
#include "SDL.h"

SDL_Surface *screen;
SDL_Surface *back;
SDL_Surface *button;
SDL_Event event;

char cIamBlank[1];
int mxpos = 0, mypos = 0, xpos = 0, ypos = 0, buttonPressed = 0;
Uint8 *keys;

void fMessagePump();
void fPrintMessages();
void fKeyPressed(int km);
void fCheckMousePos();
unsigned int fBlitImg(SDL_Surface *srcSurf, SDL_Surface *dstSurf, int w, int h, int x, int y, int x2, int y2);
unsigned int fBlitImg2(SDL_Surface *srcSurf, SDL_Surface *dstSurf, int w, int h, int x, int y, int x2, int y2);
void fLoadImages();
void fDrawScreen();

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

if ((SDL_Init(SDL_INIT_VIDEO)) != 0)
{

cout << "Could not Init Video!";

exit (0);

}
else
{

cout << "Init Video Complete!\n";

}

atexit(SDL_Quit);

SDL_WM_SetCaption("Button Test!", NULL);
SDL_WM_SetIcon(SDL_LoadBMP("C:\\Program Files\\Microsoft Visual Studio\\MyProjects\\SDL Button Test\\Debug\\Mainicon.bmp"), NULL);

cout << "Trying to create Surface: \"screen\" at 600x400x32 using Hardware with Double Buffering...\n";

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

if (screen == NULL)
{

cout << "Failed! Trying without Double Buffering...\n";

screen = SDL_SetVideoMode(600, 400, 32, SDL_HWSURFACE);

if (screen == NULL)
{

cout << "Failed! Trying Software Surface...\n";

screen = SDL_SetVideoMode(600, 400, 32, SDL_SWSURFACE);

if (screen == NULL)
{

cout << "Failed! shuting down all systems\n\n";

exit (0);

}

cout << "Surface created!\n";

}

cout << "Surface created!\n";

}

cout << "Surface created!\n";

fLoadImages();

fDrawScreen();

fMessagePump();

return 0;

}

void fMessagePump()
{

for ( ; ; )
{

if (SDL_PollEvent(&event))
{

switch (event.type)
{

case SDL_QUIT:

fPrintMessages();

break;

case SDL_KEYDOWN:

fKeyPressed(0);

break;

case SDL_MOUSEBUTTONDOWN:

buttonPressed = 1;

fKeyPressed(1);

break;

case SDL_MOUSEBUTTONUP:

buttonPressed = 0;

fKeyPressed(1);

break;

default:

break;

}

}

SDL_Delay(1);

}

}

void fPrintMessages()
{

cout << "\nMessages Printed type a charactor and hit enter to quit\n\n";

cin >> cIamBlank;

cout << "\n\n";

exit (0);

}

void fKeyPressed(int km)
{

if (km == 0)
{

keys = SDL_GetKeyState(NULL);

if (keys[SDLK_UP] == 1)
{

ypos -= 1;
fLoadImages();
fDrawScreen();

}
if (keys[SDLK_DOWN] == 1)
{

ypos += 1;
fLoadImages();
fDrawScreen();

}
if (keys[SDLK_LEFT] == 1)
{

xpos -= 1;
fLoadImages();
fDrawScreen();

}
if (keys[SDLK_RIGHT] == 1)
{

xpos += 1;
fLoadImages();
fDrawScreen();

}
if (keys[SDLK_ESCAPE] == 1)
{

fPrintMessages();

}

}
else if (km == 1)
{

if(event.button.button == SDL_BUTTON_LEFT);
{

fCheckMousePos();

}

}

return;

}

void fCheckMousePos()
{

int tempx, tempy;

tempx = ((xpos) + (button->w));
tempy = ((ypos) + (button->h));

SDL_GetMouseState(&mxpos, &mypos);

mxpos = event.button.x;
mypos = event.button.y;

if (mxpos >= xpos && mxpos <= tempx && mypos >= ypos && mypos <= tempy && buttonPressed == 1)
{

fLoadImages();

fDrawScreen();

cout << "Button Pressed!\n";

}
else if (mxpos >= xpos && mxpos <= tempx && mypos >= ypos && mypos <= tempy && buttonPressed == 0)
{

fLoadImages();

fDrawScreen();

cout << "Button Released!\n";

}

return;

}

void fLoadImages()
{

back = SDL_LoadBMP("C:\\Program Files\\Microsoft Visual Studio\\MyProjects\\SDL Button Test\\Debug\\back.bmp");

if (buttonPressed == 1)
{
button = SDL_LoadBMP("C:\\Program Files\\Microsoft Visual Studio\\MyProjects\\SDL Button Test\\Debug\\buttonP.bmp");

}
else if (buttonPressed == 0)
{

button = SDL_LoadBMP("C:\\Program Files\\Microsoft Visual Studio\\MyProjects\\SDL Button Test\\Debug\\buttonR.bmp");

}
/*
if (back == NULL)
{

cout << "Could not load background image!\n";

}
else
{

cout << "Loaded background image!\n";

}
if (button == NULL)
{

cout << "Could not load button image!\n";

}
else
{

cout << "Loaded button image!\n";

}
*/

return;

}

unsigned int fBlitImg(SDL_Surface *srcSurf, SDL_Surface *dstSurf, int w, int h, int x, int y, int x2, int y2)
{

unsigned int tempval = 0;


SDL_Rect dstRect, srcRect;

dstRect.x = x2;
dstRect.y = y2;

srcRect.h = h;
srcRect.w = w;
srcRect.x = x;
srcRect.y = y;

xpos = x2;
ypos = y2;

tempval = SDL_BlitSurface(srcSurf, &srcRect, dstSurf, &dstRect);

SDL_FreeSurface(srcSurf);

return tempval;

}

unsigned int fBlitImg2(SDL_Surface *srcSurf, SDL_Surface *dstSurf, int w, int h, int x, int y, int x2, int y2)
{

unsigned int tempval = 0;


SDL_Rect dstRect, srcRect;

dstRect.x = x2;
dstRect.y = y2;

srcRect.h = h;
srcRect.w = w;
srcRect.x = x;
srcRect.y = y;

tempval = SDL_BlitSurface(srcSurf, &srcRect, dstSurf, &dstRect);

SDL_FreeSurface(srcSurf);

return tempval;

}

void fDrawScreen()
{

unsigned int tempback, tempbutton;

tempback = fBlitImg2(back, screen, back->w, back->h, 0, 0, 0, 0);

if (xpos == 0 || ypos == 0)
{

tempbutton = fBlitImg(button, screen, button->w, button->h, 0, 0, (((screen->w) - 128) /2), (((screen->h) - 128) /2));

}
else if (xpos != 0 || ypos != 0)
{

tempbutton = fBlitImg2(button, screen, button->w, button->h, 0, 0, xpos, ypos);

}

if (tempback != 0)
{

cout << "Could not Blit background image onto Surface: \"screen\" from Surface: \"back\"!\n";

}

if (tempbutton != 0)
{

cout << "Could not Blit button image onto Surface: \"screen\" from Surface: \"button\"!\n";

}

SDL_UpdateRect(screen, 0, 0, 0, 0);

return;

}



--------------------signature--------------------

Looking for usefull, interesting resources

WELL DONT LOOK HERE>>http://www.dragonruins.com/

but its still fun to have a look around

[edited by - Smacker_626 on August 27, 2002 4:09:55 PM]

Share this post


Link to post
Share on other sites
Oh one more thing with all my SDL apps the cpu usage stays at 99 but i put an SDL_Delay(1); at the end of my loop and its whent down to 01 also in the file i gave u i didnt uncomment the SDL_FreeSurface(button); so it eat up RAM like there was no tommorow but its all fixed in these files

--------------------signature--------------------

Looking for usefull, interesting resources

WELL DONT LOOK HERE>>http://www.dragonruins.com/

but its still fun to have a look around

Share this post


Link to post
Share on other sites
Yes, i do development with SDL so I have all the dlls in my system folder, any way I way getting an access violation in fBltImg because it didn't load any bitmaps, need some error checking there. That new zip file is missing one byte, so it can't be decompressed... might want to upload it again.

[edited by - ElCrazon on August 27, 2002 4:16:55 PM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!