3 Replies - 572 Views - Last Post: 18 July 2012 - 10:57 AM Rate Topic: -----

#1 Tom_Leonardsson  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 67
  • Joined: 07-August 11

[SDL, C++] SDL_Rect won't move.

Posted 18 July 2012 - 10:23 AM

Hi!

My code does not work here it is!

//#include "headers/render.h"
//#include "src/render.cpp"
#include "sdl/sdl.h"
#include "iostream"
#include "SDL/SDL_mixer.h"
//#include "SDL/SDL_ttf"
#undef main

 bool collision(SDL_Rect* spriteC, SDL_Rect* ene2C)
    {
         if(spriteC->y >= ene2C->y + ene2C->h)
         return 0;
         if(spriteC->x >= ene2C->x + ene2C->w)
         return 0;
         if(spriteC->y + spriteC->h <= ene2C->y)
         return 0;
         if(spriteC->x + spriteC->w <= ene2C->x)
         return 0;
        return 1;
    }

const int WINDOW_WIDTH = 600;
const int WINDOW_HEIGHT = 480;
const char* WINDOW_TITLE = " Dragons' Realm ";
void drawSprite(SDL_Surface* imageSurface,
                SDL_Surface* screenSurface,
                int srcX, int srcY,
                int dstX, int dstY,
                int width, int height);
using namespace std;



int main(int argc, char **argv)
{
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO);
   SDL_Surface* screen = SDL_SetVideoMode( WINDOW_WIDTH, WINDOW_HEIGHT, 0, SDL_HWSURFACE | SDL_DOUBLEBUF );
   SDL_WM_SetCaption( WINDOW_TITLE, 0 );
   // Imagese
SDL_Surface* back2 = SDL_LoadBMP("back2.bmp");
SDL_Surface* bitmap = SDL_LoadBMP( "sprite.bmp" );
SDL_Surface* temp = SDL_LoadBMP("back.bmp");
SDL_Surface* cave = SDL_LoadBMP("cave.bmp");
SDL_Surface* clicktostart = SDL_LoadBMP("clicktostart.bmp");
SDL_Surface* sprite = NULL;
SDL_Surface* pal = NULL;
SDL_Surface* bul = NULL;
SDL_Surface* enemy = NULL;
SDL_Surface* enemy2 = NULL;
SDL_Surface* spriteC;
SDL_Surface* bg = SDL_DisplayFormat(temp);
SDL_FreeSurface(temp);

   SDL_SetColorKey(bitmap, SDL_SRCCOLORKEY, SDL_MapRGB(bitmap->format, 255, 0, 255));
   SDL_SetColorKey(cave, SDL_SRCCOLORKEY, SDL_MapRGB(cave->format, 255, 0, 255));
   SDL_SetColorKey(clicktostart, SDL_SRCCOLORKEY, SDL_MapRGB(clicktostart->format, 255, 0, 255));
   // Player
   int spriteImageX = 0;
   int spriteImageY = 31;
   int spriteWidth =  100;
   int spriteHeight = 100;
   int spriteX = 250;
   int spriteY = 200;
   // Enemy
   int eneImageX = 425;
   int eneImageY = 302;
   int eneWidth = 100;
   int eneHeight = 100;
   int eneX = 268;
   int eneY = 224;
   // enemy2
   int ene2ImageX = 11;
   int ene2ImageY = 342;
   int ene2Width = 100;
   int ene2Height = 100;
   int ene2X = 500;
   int ene2Y = 100;
   // foreground
   int caveImageX = 117;
   int caveImageY = 1;
   int caveWidth = 800;
   int caveHeight = 600;
   int caveX = 900;
   int caveY = 1;
   // Pally
   int palImageX = 17;
   int palImageY = 274;
   int palWidth = 50;
   int palHeight = 50;
   int palX = 105;
   int palY = 100;
   // Bullet
   int bulImageX = 441;
   int bulImageY = 77;
   int bulWidth = 12;
   int bulHeight = 8;
   int bulX = 10000;
   int bulY = 10000;
   // Temp
   int bgImageX = 1;
   int bgImageY = 1;
   int bgWidth = 640;
   int bgHeight = 480;
   int bgX = 1;
   int bgY = 1;
   // back2
   int bg2ImageX = 1;
   int bg2ImageY = 1;
   int bg2Width = 2560;
   int bg2Height = 480;
   int bg2X = 0;
   int bg2Y = 0;
   // spriteC
   int spriteCX = 250;


   SDL_Event event;
   bool gameRunning = true;
   bool keysHeld[323] = {false};
   while (gameRunning)
   {

      if (SDL_PollEvent(&event))
      {
         if (event.type == SDL_QUIT)
         {
            gameRunning = false;
         }
        // keyinput
         if (event.type == SDL_KEYDOWN)
         {
            keysHeld[event.key.keysym.sym] = true;
         }
         if (event.type == SDL_KEYUP)
         {
            keysHeld[event.key.keysym.sym] = false;
         }

      }

      /////////////////////////////////////////////////////
      // KEYINPUT!                                       //
      /////////////////////////////////////////////////////

      if ( keysHeld[SDLK_ESCAPE] )
      {
         gameRunning = false;
      }

      if (keysHeld[SDLK_RIGHT])
      {
          spriteX += 1;
      }
      if(keysHeld[SDLK_LEFT])
      {
          spriteX -= 1;
      }
      if (keysHeld[SDLK_UP])
      {
          spriteY -= 1;
      }

      if(keysHeld[SDLK_DOWN])
      {
          spriteY += 1;
      }
          if(keysHeld[SDLK_SPACE])
      {
          bulX = spriteX+20;//(100 + spriteWidth / 2, 100 + spriteHeight / 2);
          bulY = spriteY;//(100 + spriteWidth / 2, 100 + spriteHeight / 2);
          cout << "SHOOTING! \n";
      }

            if(keysHeld[SDLK_p])
      {
          palX = spriteX;
          palY = spriteY;
      }

      if (keysHeld[SDLK_r])
      {
             eneX = 259;
             eneY = 184;

             ene2X = 500;
             ene2Y = 100;

             cout << "Restarting enemys \n";
      }
      if(keysHeld[SDLK_l])
      {
             bulX = 1;
             bulY = 1;
      }
       if(keysHeld[SDLK_ESCAPE])
       {
            cout << "exit\n";
       }

       if(keysHeld[SDLK_m])
       {
           spriteImageX = 224;
           spriteImageY = 36;
           cout << "sLASH\n";
       }
       if(keysHeld[SDLK_s])
       {
           SDL_Delay(100);
       }
      // key up
else if(event.type == SDL_KEYUP)
        {
			if(event.key.keysym.sym == SDLK_m)
				spriteImageX = 1, spriteImageY = true;

        }



   /* update the screen */
   SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
   SDL_BlitSurface(back2, NULL, screen, NULL);
  // SDL_BlitSurface(cave, NULL, screen, NULL);
   SDL_BlitSurface(bul, NULL, screen, NULL);
   SDL_BlitSurface(enemy, NULL, screen, NULL);
   SDL_BlitSurface(pal, NULL, screen, NULL);


drawSprite(back2,
                 screen,
                 bg2ImageX, bg2ImageY,
                 bg2X, bg2Y,
                 bg2Width, bg2Height);
drawSprite(bitmap,
                 screen,
                 bulImageX, bulImageY,
                 bulX, bulY,
                 bulWidth, bulHeight);

drawSprite(bitmap,
                 screen,
                 spriteImageX, spriteImageY,
                 spriteX, spriteY,
                 spriteWidth, spriteHeight);

drawSprite(bitmap,
                 screen,
                 eneImageX, eneImageY,
                 eneX, eneY,
                 eneWidth, eneHeight);



drawSprite(bitmap,
                 screen,
                 palImageX, palImageY,
                 palX, palY,
                 palWidth, palHeight);


drawSprite(bitmap,
                 screen,
                 ene2ImageX, ene2ImageY,
                 ene2X, ene2Y,
                 ene2Width, ene2Height);



drawSprite(cave,
                 screen,
                 caveImageX, caveImageY,
                 caveX, caveY,
                 caveWidth, caveHeight);

SDL_Rect spriteC = {500,100,49,65};
SDL_FillRect(screen,&spriteC, SDL_MapRGB(screen->format, 255, 0, 255));
spriteC.x = 250;
spriteC.y = 200;
spriteC.w = 49;
spriteC.h = 65;
SDL_Rect ene2C = {500,100,100,100};
SDL_FillRect(screen,&ene2C, SDL_MapRGB(screen->format, 255, 0, 255));
ene2C.x = 500;
ene2C.y = 100;
ene2C.w = 100;
ene2C.h = 100;
SDL_Rect bulC = {10000,10000,50,50};
SDL_FillRect(screen,&bulC, SDL_MapRGB(screen->format, 255, 0, 255));
bulC.x = 10000;
bulC.y = 10000;
bulC.w = 50;
bulC.h = 50;

    ene2C.x += 1;
    bg2X -= 1;
    caveX -= 1;
    ene2X -= 1;
    bulX += 2;
    eneX -= 1;

    if(!collision(&spriteC,&ene2C))
     {
        // cout << "collision detected \n";

     }

    if( bg2X <= -1920 )
        {
            //Reset the offset
            bg2X = 1;
        }


    if(ene2Y >= spriteY)
       {
        ene2Y -= 1;
       }

    if(bulX <= 640)
    {
        keysHeld[SDLK_SPACE] = false;
    }

    if(ene2Y >= spriteY);
    else ene2X -= 1;

    if(spriteX >= 1);
    else spriteX += 1;

    if(spriteX <= 550);
    else spriteX -= 1;

    if(spriteY >= 5);
    else spriteY += 1;

    if(spriteY <= 430);
    else spriteY -= 1;

    if(ene2X >= 1);
    else ene2X = 681;

    if(spriteX <= ene2X);
    else ene2Y += 1;

    if(ene2X >= 1);
    else ene2X = 681;

    if(ene2X >= 1);
    else ene2Y = 50;

    if(caveX >= -640);
    else  caveX = 900;

    if(bulX >= 640)
    {
        bulX = spriteX;
        bulY = spriteY;
        cout << rand();
        cout << "dm \n";
    }
SDL_Flip(screen);

}



   SDL_FreeSurface(bitmap);
   SDL_FreeSurface(cave);
   SDL_FreeSurface(temp);
   SDL_FreeSurface(bul);
   SDL_FreeSurface( enemy );
   SDL_FreeSurface(pal);
   SDL_FreeSurface(bg);
   SDL_FreeSurface(temp);


   SDL_Quit();

   return 0;
}
void drawSprite(SDL_Surface* imageSurface,
                SDL_Surface* screenSurface,
                int srcX, int srcY,
                int dstX, int dstY,
                int width, int height)


{
   SDL_Rect srcRect;
   srcRect.x = srcX;
   srcRect.y = srcY;
   srcRect.w = width;
   srcRect.h = height;
   SDL_Rect dstRect;
   dstRect.x = dstX;
   dstRect.y = dstY;
   dstRect.w = width;
   dstRect.h = height;
   SDL_BlitSurface(imageSurface, &srcRect, screenSurface, &dstRect);
}



Is This A Good Question/Topic? 0
  • +

Replies To: [SDL, C++] SDL_Rect won't move.

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3458
  • View blog
  • Posts: 10,665
  • Joined: 05-May 12

Re: [SDL, C++] SDL_Rect won't move.

Posted 18 July 2012 - 10:28 AM

Telling us "it doesn't work" is not very useful. Describe the problem. Tell us where you have narrowed the problem to. Tell us what you have tried to do to fix it.
Was This Post Helpful? 0
  • +
  • -

#3 Tom_Leonardsson  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 67
  • Joined: 07-August 11

Re: [SDL, C++] SDL_Rect won't move.

Posted 18 July 2012 - 10:43 AM

View PostSkydiver, on 18 July 2012 - 10:28 AM, said:

Telling us "it doesn't work" is not very useful. Describe the problem. Tell us where you have narrowed the problem to. Tell us what you have tried to do to fix it.


The SDL_Rects ene2C, spriteC wont move and here's the code for them.

SDL_Rect spriteC = {500,100,49,65};
SDL_FillRect(screen,&spriteC, SDL_MapRGB(screen->format, 255, 0, 255));
spriteC.x = 250;
spriteC.y = 200;
spriteC.w = 49;
spriteC.h = 65;
SDL_Rect ene2C = {500,100,100,100};
SDL_FillRect(screen,&ene2C, SDL_MapRGB(screen->format, 255, 0, 255));
ene2C.x = 500;
ene2C.y = 100;
ene2C.w = 100;
ene2C.h = 100;
SDL_Rect bulC = {10000,10000,50,50};
SDL_FillRect(screen,&bulC, SDL_MapRGB(screen->format, 255, 0, 255));
bulC.x = 10000;
bulC.y = 10000;
bulC.w = 50;
bulC.h = 50;

    ene2C.x += 1;
    bg2X -= 1;
    caveX -= 1;
    ene2X -= 1;
    bulX += 2;
    eneX -= 1;


Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3458
  • View blog
  • Posts: 10,665
  • Joined: 05-May 12

Re: [SDL, C++] SDL_Rect won't move.

Posted 18 July 2012 - 10:57 AM

It's because you keep resetting the value each time you loop around. See lines 281 and 283 of your original post.

And as for spriteC, you never change any values on it. Why are you expecting it to move?

Or here's a small example of what you are doing:
#include <iostream>

using namespace std;

struct MyRect
{
    int x, y;
    int w, h;
};

int main()
{
    int n = 10;
    while(n-- > 0)
    {
        MyRect rect = { 5, 23, 60, 10 };
        rect.x += 1;
        cout << rect.x << endl;
    }

    return 0;
}



If you used proper indents in your code, it would have stood out that you are initializing the values within your loop. I recommend picking an indent style and applying it consistently.

This post has been edited by Skydiver: 18 July 2012 - 12:49 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1