Jump to content

  • Log In with Google      Sign In   
  • Create Account


munchor

Member Since 17 Dec 2011
Offline Last Active May 19 2012 02:49 PM
-----

Topics I've Started

[SDL] Moving right and left

19 May 2012 - 12:08 PM


  while (running) {
	SDL_PollEvent(&event);

	if (event.type == SDL_QUIT) {
	  SDL_Quit();
	  return 0;
	} else if (event.type == SDL_KEYDOWN) {
	  int key_code = event.key.keysym.sym;

	  if (key_code == SDLK_ESCAPE) {
		SDL_Quit();
		return 0;
	  }

	  if (key_code == SDLK_RIGHT && player.x < SCREEN_WIDTH - 5 - player.width) player.x++;
	  if (key_code == SDLK_LEFT && player.x > 0 + 5) player.x--;
	}

	draw_rect(screen, player.x, player.y, player.width, player.height, 0xFF);

	SDL_Flip(screen);
	clear_screen(screen, 0, 0, 0);
  }

This is my main code loop, and it's where I make my player move right or left. However, when I press right, and then release right and press left (fairly quick movement) again, the player will move a bit to the left a bit and then stops, requiring me to release LEFT and repress it to keep moving.

The player gets stuck all the time, it's hard to explain what's going on.

Any ideas on how can I fix this? Thank you in advance.

When to free sound

03 March 2012 - 04:48 AM

I'm using SDL_mixer to play a .wav sound, but I don't know when to free it.

I have this code:

Bullet::Bullet() {
  //Create Bullet

  /* Play the shot sound */
  sound = Mix_LoadWAV("media/gunshot.wav"); //sound is a private property of Bullet

  if (sound == NULL) {
	fprintf(stderr, "Unable to load WAV file: %s\n", Mix_GetError());
  }

  Mix_PlayChannel(0, sound, 0);
}

My problem is that if I free the sound right after I play it, it will not be played.
If I free the sound when the bullet is deleted (deconstructor), the sound stops before it is over, because I delete bullets whenever they leave the screen, and sometimes they leave the screen before the sound isn't over.

So, my doubt it, how can I handle loading the sound and freeing it without super RAM consumption? Thank you!

Moving diagonally

21 February 2012 - 12:06 PM

I'm having an issue with moving diagonally. Firstly, I don't want my player to move faster when moving diagonally. Secondly, I want to mimic the movement of most modern video games (think about first person shooters, that is the kind of movement I want, although my game is 2D).

This is my main game loop:


while (running) {
    SDL_PollEvent(&event);
      if (event.type == SDL_QUIT) {
        SDL_Quit();
        return 0;
      }

      if (event.type == SDL_KEYDOWN) {
        switch (event.key.keysym.sym) {
          case SDLK_LEFT:
            player.vx = -1;
            break;
          case SDLK_RIGHT:
            player.vx = 1;
            break;
          case SDLK_DOWN:
            player.vy = 1;
            break;
          case SDLK_UP:
            player.vy = -1;
            break;
          case SDLK_ESCAPE:
            SDL_Quit();
            return 0;
            break;
        }
      }

    player.draw(screen);

    SDL_Flip(screen);
    clear_screen(screen);
  }

I have that, but I also tried to add a (if event.type == SDL_KEYUP), and then for cases UP and DOWN, set vy = 0, RIGHT and LEFT, set vx = 0. However, that didn't work so well either. I also tried using both SDL_PollEvent(&event); and while (SDL_PollEvent(&event)) {}.

I tried combinations of those too, and I'm updating "player.x += player.vx", and "player.y += player.vy;", but I just can't get proper diagonal movement.

Any idea of what I can try? Thank you in advance!

SDL and C++ Classes

20 February 2012 - 09:48 AM

I am having an issue with SDL and C++ classes. Basically, I want my Player to have a class of its own, so I made this (Player.cpp):

#include <SDL/SDL.h>

class Player {
  int x;
  int y;
};

I've got my main file, with the main () function, and the drawing functions. I wanted to call something like "Player player = new Player ();" (Java, Python and other languages allow me to do something like this).

I guessed that I would have to compile Player.cpp first, but it complains about not having a main function and whatnot.

So, I have a few questions:
- How can I have my own SDL Player class, and associate it with one or more sprites;
- I heard that I need header files to link the files, but I have no idea how;
- How to compile the different files, I am using a Makefile to build the game btw;

Thank you tons in advance.

Oh, and regarding question #1, I would also like to know how I can call the Player class from the main file so that then I can draw it.

Free movement and collision

20 December 2011 - 06:18 AM

First of all, this is partially a Maths/Physics question, and partially an SDL question, hence I'm not sure on which forum to post this. If it's wrong, feel free to move it.

I was developing a game based on a grid system, and the player could only move 25 pixels per click, and when the player held the key for too long, it would simply move one tile, and then stay in the same place, instead of moving more tiles. It's a maze game, so I'm not sure of how to proceed.

I would like the player to be able to move freely, since it always looks better and enhances gameplay, but I'm not sure of how to combine a grid map (with things on the floor, doors, etc.) and a free movement. In fact, I'm not even sure if it's possible.

Here's some of my code (possibly impossible to run on its own)

int main(int argc, char *argv[])
{
  /* Initialize SDL */
  SDL_Surface *screen;
  SDL_Init(SDL_INIT_VIDEO);
  screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_DEPTH, SDL_SWSURFACE);
  SDL_WM_SetCaption("test", "test");

  int map[MAP_HEIGHT][MAP_WIDTH];
  SDL_Event event;

  load_map();

	while (true)
	{
	  while (SDL_PollEvent(&event))
	  {
		if (event.type == SDL_QUIT)
		{
		  SDL_Quit();
		  return 0;
		}

		if (event.type == SDL_KEYDOWN)
		{
		  /* This is where I get key input, no acceleration, just
			   	"player_y = player_y - TILE_SIZE"
		  */
		}
(...)


On the one hand, I would like a way of moving freely like in a first person shooter game, or Animal Crossing (most commercial games with a moving player actually), but on the other hand, can I still have a map grid? I'm not sure and I need help both with deciding what to use in a maze game, and with the code (continuous key pressing).

Actually and ideally, how to move one tile at a time, but when he keeps pressing the key for longer, the player will keep moving more tiles? Thanks.

PARTNERS