Jump to content
  • Advertisement

CraazyDave

Member
  • Content Count

    23
  • Joined

  • Last visited

Community Reputation

100 Neutral

About CraazyDave

  • Rank
    Member
  1. CraazyDave

    A stupid SDL_Surface height and width question

    Ok, just to clarify. I set two integers in Image, h and w, from the height and width from the SDL_Surface *. At that point the values are correct. But then in the Sprite class I call two methods in Image that returns the height and width from Image. And it is from that point that the values gets all wrong. So yes. The values are at one point right. *Edit* I still have not figured out why it did not work as it should but I have found a workaround. Instead of sending the height and width from Image to Sprite I retrieve the surface from Image and then in sprite does "surface -> w;" and "surface -> h;" And that works.
  2. CraazyDave

    A stupid SDL_Surface height and width question

    Ok first I have to explain that the SDL_Surface * exists in a class called Image that also keeps controll of the refcount of the surface amongst other things which the Sprite class then uses. Height and width are both Integers. Now when I set the height and width in the Image constructor they become when I tested it: 375 and 100 in height and width respectively. That looks right to me. So I checked what that value becomes when it gets to the Sprite class and it then turns into: 8 and 1970344334. That's a big difference.
  3. I am making a simple Pong game in C++ with SDL and I need to get the height and width of my Sprite and I should be able to get that from my SDL_Surface * that is the visual representation of the Sprite. Now I am doing that right now simply by doing it like this: h = opPicture -> h; w = opPicture -> w; Where opPicture is the SDL_Surface in question. But I get some really weird values as a result. Am I doing it wrong, is there another way to get those values?
  4. CraazyDave

    Trouble moving sprites

    Yes, the function works perfectly fine. The thing is that now update() takes about twice as long time to execute than if you removed one of the calls. This might slow your program down. Maybe it's not a problem for a simple game but I still think it's a bit wasteful for no good reason. [/quote] I have done as you suggested with the update and deconstructor in GameEngine. It all works perfectly. I Am about to start the major changes that you suggested.
  5. CraazyDave

    Trouble moving sprites

    Thanks for the tip! I am having a hard time figuring out pointers, are so used to coding in Java that I becomes difficult to use pointers for me. But you sure have a good point. I'll try to send the pointer to the things I am sending instead of a copy. And then see what happens. And to other issues you mention. checkFps() should controll the framerate but if it allways returns 33 then It does not seem to do that. The update function works, maybe I will change it later if I feel like it but it does not affect the program negatively as far as I know. Good point about GameEngine::exit(), I'll do that
  6. CraazyDave

    Trouble moving sprites

    Hi! I don't think that is true. From 0 to < spriteField.size() there are n elements. From 0 to <= spriteField.size() the are n+1 elements. This means, there is one more sprite which he has not added in the vector. This should throw an index out of bounds exception. [/quote] This is correct, the for loop is fine unless C++ has somehow changed it's size function since I last used it. [/quote] Yes well regardless how the for loop looks it seems to work. But still no movement
  7. I have been struggling with the process of making my Sprite objects to move at all in my game. It works fine when I write all the SDL related code in the main but I'm trying to seperate the main from the SDL related code in another class. I can blit the Sprite with no problem but when I try to move the sprite by pressing the UP, DOWN, LEFT, RIGHT keys it does not work. Now I started another thread in the beguinners section, perhaps this belongs there as well but my issue is SDL related. What I think I need is a example of how this can be achieved, moving sprites in SDL without having any SDL code in the main. Or some help figuring out what's wrong with my code. What ever suits you guys.
  8. CraazyDave

    Trouble moving sprites

    You know what. Having the rendering in the Sprite class may not be impossible. I'll at least try it before I try something else or post another thread!
  9. CraazyDave

    Trouble moving sprites

    I agree. This is getting to messy. And I have posted the code for rendering a sprite. But for clarification: In the while loop in the main i call the method blit from my GameEngine object. g.blit(s1.getImage().getSurface(), s1.getRect().get()); where the first argument is the SDL_Surface * that contains the acutal image. The other argument is the rect that contains the x and y values that should change depending on my keypresses. If you want to see more of the rendering of the sprite then I do not see the point since it works and is unrelated to the issue. The issue is that the rect does not change at all when it should. The setRect is made in the move method in Sprite which is called in the events method in GameEngine: void Sprite::move() { x += xVel; y += yVel; std::cout << "move is" << x << " " << y << std::endl; rect.setRect(x, y); } Now since this is, as you said, getting messy perhapos I should start a new thread asking instead how I should implement the movement of a Sprite as I want it since this is not getting anywhere.
  10. CraazyDave

    Trouble moving sprites

    Well I call the setVel method that sets what the velocity of the sprite should be in the events() method, and now that I look at it it perhaps should be xVel = xv not x += xv but I digress. And directly after that I call the move method() which in turn changes the x and y values. And they are changed correctly when everytime I check. By the way the move method I posted was incomplete from what I have been testing. I forgot to add the rect.setRect(x, y); At the end of the method. And rect is of a class that contains a SDL_Rect and setRect(x, y); sets the x and y values of the rect. And that rect are then taken in the main method for the blitting of the sprite. Damn this is getting messy. SO what is happening is that the keypresses are working but the rect does not change. [/quote] If it's not clear why this does not work, how would you achieve what I'm trying to do here? With moving a sprite while keeping all the SDL functionality away from the main. Btw, I have gotten everything to work when I hardcode a movement for the sprites or if I have written the event loop in the main instead.
  11. CraazyDave

    Trouble moving sprites

    Well I call the setVel method that sets what the velocity of the sprite should be in the events() method, and now that I look at it it perhaps should be xVel = xv not x += xv but I digress. And directly after that I call the move method() which in turn changes the x and y values. And they are changed correctly when everytime I check. By the way the move method I posted was incomplete from what I have been testing. I forgot to add the rect.setRect(x, y); At the end of the method. And rect is of a class that contains a SDL_Rect and setRect(x, y); sets the x and y values of the rect. And that rect are then taken in the main method for the blitting of the sprite. Damn this is getting messy. SO what is happening is that the keypresses are working but the rect does not change. [/quote] If it's not clear why this does not work, how would you achieve what I'm trying to do here? With moving a sprite while keeping all the SDL functionality away from the main. Btw, I have gotten everything to work when I hardcode a movement for the sprites or if I have written the event loop in the main instead.
  12. CraazyDave

    Trouble moving sprites

    Well I call the setVel method that sets what the velocity of the sprite should be in the events() method, and now that I look at it it perhaps should be xVel = xv not x += xv but I digress. And directly after that I call the move method() which in turn changes the x and y values. And they are changed correctly when everytime I check. By the way the move method I posted was incomplete from what I have been testing. I forgot to add the rect.setRect(x, y); At the end of the method. And rect is of a class that contains a SDL_Rect and setRect(x, y); sets the x and y values of the rect. And that rect are then taken in the main method for the blitting of the sprite. Damn this is getting messy. SO what is happening is that the keypresses are working but the rect does not change.
  13. CraazyDave

    Trouble moving sprites

    That wont have an effect, as the event will only return a result once per call, AKA, it's not going to change during the processing of that code. However, you are onto what I think the problem is. In the event loop you are increasing the velocity on key down, but then decrementing velocity on key up. That means on a key press ( an down and up event ) you are immediately increasing the velocity, then in the very next event literally milliseconds away, you are descreasing velocity. So, in the event of a keypress, the following is happening. velocity set to 0 key down, velocity increased by 5 milliseconds later, next event in loop is keyup event, velocity reduced back to 0. This is most likely not the logic you actually want. That said, in the event of a key held down, your sprite should move. In this case, without seeing more code ( specifically setVel() and move() ) there is no way to know eactly where the problem is. [/quote] Basically what you stated is exactly what I was thinking about the events canceling each other but I am not sure why I thought the else if would fix that. As for the problem Crazy, you are probably still having the same problem even though they are split into separate functions because the way you are handling the events is probably the same...on key down; do this, on key up; do this. You may want to post how you are calling these functions. Also a little background on the type of game might help, reading the code I am assuming it is a sort of racing game or space game and that is why you have -velocity calculations on key up opposed to just stopping the sprite all together, correct? [/quote] Allright. I did some thinking and I figure that the else if is unnecessary so I commented it away to try and see what happened. RIght now what happens everytime I press a key I see that it changes the xVel and yVel in setVel() and changes are made to the values affected in move() but when the values are taken to the bliting method they are the of the dafault value. If that makes any sense. I'll just post the classes that is involved in the movement: GameEngine.cpp: #include "SDL.h" #include "GameEngine.h" #include "Image.h" #include "Sprite.h" #include <vector> #include <string> #include <iostream> GameEngine::GameEngine(int h, int w) { SCREEN_HEIGHT = h; SCREEN_WIDTH = w; SDL_Init(SDL_INIT_EVERYTHING); screen = SDL_SetVideoMode(SCREEN_HEIGHT, SCREEN_WIDTH, 32, SDL_SWSURFACE | SDL_DOUBLEBUF); SDL_ShowCursor(SDL_DISABLE); } void GameEngine::log(std:: string log) { std::cout << log.c_str() << std::endl; } void GameEngine::update() { //Update screen and rect SDL_Flip(screen); SDL_UpdateRect(screen, 0, 0, 0, 0); } void GameEngine::fill() { SDL_FillRect(getScreen(), NULL, 0x000000); } void GameEngine::blit(SDL_Surface * i, SDL_Rect rect) { SDL_BlitSurface(i, NULL, screen, &rect); } void GameEngine::exit() { SDL_Quit(); } bool GameEngine::events() { std::cout << spriteField.size() << std::endl; int xVel = 0; int yVel = 0; while(SDL_PollEvent(&event)) { if(event.type == SDL_QUIT) { exit(); return true; } if(event.type == SDL_KEYDOWN) { switch(event.key.keysym.sym) { case SDLK_UP: yVel -= 5; break; case SDLK_DOWN: yVel += 5; break; case SDLK_RIGHT: xVel -= 5; break; case SDLK_LEFT: xVel += 5; break; } } /*else if(event.type == SDL_KEYUP) { switch(event.key.keysym.sym) { case SDLK_UP: yVel += 5; break; case SDLK_DOWN: yVel -= 5; break; case SDLK_RIGHT: xVel += 5; break; case SDLK_LEFT: xVel -= 5; break; } }*/ for(int i = 0; i < spriteField.size(); i++) { spriteField.setVel(xVel, yVel); spriteField.move(); } } return false; } int GameEngine::checkFPS() { const int tickInterval = 1000/30; nextTick = SDL_GetTicks() + tickInterval; delay = nextTick - SDL_GetTicks(); return delay; } void GameEngine::delayProgram(int delay) { if(delay > 0) SDL_Delay(delay); } SDL_Surface * GameEngine::getScreen() { return screen; } void GameEngine::addSprite(Sprite s) { spriteField.push_back(s); } SDL_Rect GameEngine::createTempRect(int x, int y) { SDL_Rect rectTemp; rectTemp.x = x; rectTemp.y = y; return rectTemp; } Sprite.cpp: Sprite::Sprite(int xv, int yv, bool b) { xVel = 0; yVel = 0; player = b; x = xv; y = yv; rect.setRect(x, y); w = rect.getW(); h = rect.getH(); } void Sprite::setImage(Image img) { i = img; } Image Sprite::getImage() { return i; } void Sprite::tick(std::vector<Sprite> spriteField) { for(int i = 0; i < spriteField.size(); i++) collision(spriteField); } void Sprite::setVel(int xv, int yv) { xVel += xv; yVel += yv; std::cout << "setVel" << x << " " << y << std::endl; } void Sprite::move() { x += xVel; y += yVel; std::cout << "move is" << x << " " << y << std::endl; } bool Sprite::collision(Sprite other) { int leftA, leftB; int rightA, rightB; int topA, topB; int bottomA, bottomB; //Calculate the sides of rect A leftA = x; rightA = x + w; topA = y; bottomA = y + h; //Calculate the sides of rect B leftB = other.x; rightB = other.x + other.w; topB = other.y; bottomB = other.y + other.h; //If any of the sides from A are outside of B if(bottomA < topB) { return false; } if(topA > bottomB) { return false; } if(rightA < leftB) { return false; } if(leftA > rightB) { return false; } //If none of the sides from A are outside B return true; } void Sprite::log(std::string log) { std::cout << log << std::endl; } Rect Sprite::getRect() { return rect; } int Sprite::getX() { return x; } int Sprite::getY() { return y; } and the main.cpp: int main(int argc, char * args[]) { GameEngine g(1000, 800); bool quit = false; //Create images. Image i(true, "pong_player.bmp"); Image background(false, "pong_middle.bmp"); //Sprite Sprite s1(150, 200, true); s1.setImage(i); g.addSprite(s1); while(quit == false) { int delay = g.checkFPS(); //Fill screen with black g.fill(); g.blit(s1.getImage().getSurface(), s1.getRect().get()); //Blit the background g.blit(background.getSurface(), g.createTempRect(500, 0)); //Update the screen g.update(); //Check for events quit = g.events(); g.delayProgram(delay); } g.exit(); return 0; } And I'm not trying to make a particularly sofisticated game, I'm trying to make a reversed version of Pong where you try to avoid the ball instead of hitting it.
  14. CraazyDave

    Trouble moving sprites

    That wont have an effect, as the event will only return a result once per call, AKA, it's not going to change during the processing of that code. However, you are onto what I think the problem is. In the event loop you are increasing the velocity on key down, but then decrementing velocity on key up. That means on a key press ( an down and up event ) you are immediately increasing the velocity, then in the very next event literally milliseconds away, you are descreasing velocity. So, in the event of a keypress, the following is happening. velocity set to 0 key down, velocity increased by 5 milliseconds later, next event in loop is keyup event, velocity reduced back to 0. This is most likely not the logic you actually want. That said, in the event of a key held down, your sprite should move. In this case, without seeing more code ( specifically setVel() and move() ) there is no way to know eactly where the problem is. [/quote] I changed it to else if, and I can agree why that would be preferable but it made no difference. So here is the setVel() and Move() void Sprite::setVel(int xv, int yv) { xVel += xv; yVel += yv; std::cout << "setVel" << x << " " << y << std::endl; } And: void Sprite::move() { x += xVel; y += yVel; std::cout << "move is" << x << " " << y << std::endl; } And nothing special happens in them. I could poste the entire main, GameEngine and Sprite class if needed
  15. CraazyDave

    Trouble moving sprites

    I am trying to make a game in SDL but I am avoiding at all costs to have any part of SDL in the main. Instead I have a class GameEngine with all the SDL functionality. My problem is that I need to tie together the keyinputs that I check in GameEngine and call in the main method to the blitting of the Sprite. It all should be straightforward but for some reason it does not work for me. Now the problem I am having is that the x and y values that I use to change the position of the Sprite does not update with my keypresses. The Method that checks the events: bool GameEngine::events() { std::cout << spriteField.size() << std::endl; int xVel = 0; int yVel = 0; while(SDL_PollEvent(&event)) { if(event.type == SDL_QUIT) { exit(); return true; } if(event.type == SDL_KEYDOWN) { switch(event.key.keysym.sym) { case SDLK_UP: yVel -= 5; break; case SDLK_DOWN: yVel += 5; break; case SDLK_RIGHT: xVel -= 5; break; case SDLK_LEFT: xVel += 5; break; } } if(event.type == SDL_KEYUP) { switch(event.key.keysym.sym) { case SDLK_UP: yVel += 5; break; case SDLK_DOWN: yVel -= 5; break; case SDLK_RIGHT: xVel += 5; break; case SDLK_LEFT: xVel -= 5; break; } } for(int i = 0; i < spriteField.size(); i++) { spriteField.setVel(xVel, yVel); spriteField.move(); } } return false; } Any help with figuring out why it does not work would be very helpfull.
  • 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!