Advertisement Jump to content
  • Advertisement


  • Content Count

  • Joined

  • Last visited

Community Reputation

101 Neutral

About munchor

  • Rank
  1. munchor

    [SDL] Moving right and left

    Servant Of The Lord, I actually read what you said twice, very nice explanation, I shall implement it right away and see how it works
  2. 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.
  3. munchor

    When to free sound

    Thank you, the first alternative worked!
  4. munchor

    When to free sound

    The problem is how can I play them from the Bullet class if I do that.
  5. When you use SDL_BlitSurface you can also change the size of the image... Maybe you should take a look at
  6. You shouldn't loop the image and draw it pixel by pixel... SDL_RWops *gold_rwops = SDL_RWFromFile("img/gold.png", "rb"); SDL_Surface *gold = IMG_LoadPNG_RW(gold_rwops); SDL_Rect tile = {250 + x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE}; SDL_BlitSurface(gold, NULL, screen, &tile); Here's how I did it.
  7. munchor

    When to free sound

    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!
  8. As I recall, you can set alpha: //SDL_SetAlpha(surface_name, SDL_SRCALPHA, SDL_ALPHA_TRANSPARENT); //SDL_DisplayFormatAlpha(surface_name); However, I never needed to use it to get true transparency.
  9. munchor

    Moving diagonally

    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!
  10. munchor

    SDL and C++ Classes

    Thank you, I already fixed everything =D
  11. munchor

    SDL and C++ Classes

    Ah, so the {} was defining it already, I get it now. Dropping the {} fixed it (and adding the arguments as well, but I had already done it ;)). My problem now is that on my Game.cpp, I have "#include "Player.h"", and I call: Player player(20, 20); However, the compiler returns: In function `main': Game.cpp:(.text+0x133): undefined reference to `Player::Player(int, int)' collect2: ld returned 1 exit status I have Player.o on the directory, am I forgetting some linking, I am using this to compile: g++ Game.cpp -o Game `sdl-config --cflags --libs` -lSDL_mixer -lSDL_ttf -lSDL_image I have all that because of SDL, yes.
  12. munchor

    SDL and C++ Classes

    I'm using g++, and no IDE. I can compile Player to a .o file like this: g++ -c Player.cpp `sdl-config --cflags --libs` -lSDL_mixer -lSDL_ttf -lSDL_image And I can compile my game with: g++ Game.cpp -o Game `sdl-config --cflags --libs` -lSDL_mixer -lSDL_ttf -lSDL_image But my problem is the errors I get: Player.cpp:3:1: error: redefinition of ‘Player::Player(int, int)’ Player.h:8:5: error: ‘Player::Player(int, int)’ previously defined here I am using #ifnedf and #define to avoid this kind of errors, though, what's wrong?
  13. munchor

    SDL and C++ Classes

    Thank you for the article rip off! Player.h #ifndef PLAYER_H #define PLAYER_H class Player { private: Player() {}; public: int x; int y; }; #endif Player.cpp #include "Player.h" Player::Player (int player_x, int player_y) { x = player_x; y = player_y; } Game.cpp #include <stdio.h> #include <SDL/SDL.h> #include <SDL/SDL_image.h> #include "Player.h" #define SCREEN_WIDTH 800 #define SCREEN_HEIGHT 600 #define SCREEN_DEPTH 8 using namespace std; int main(int argc, char *argv[]); void draw_player(SDL_Surface *screen, int x, int y); void clear_screen(SDL_Surface *screen); void draw_player(SDL_Surface *screen, int x, int y) { /* Blits the player to the screen */ SDL_Rect player_block = {x, y, 25, 25}; Uint8 player_color = SDL_MapRGB(screen->format, 255, 255, 255); SDL_FillRect(screen, &player_block, player_color); } void clear_screen(SDL_Surface *screen) { /* Clear the screen to black */ Uint8 color_black = SDL_MapRGB(screen->format, 0, 0, 0); SDL_FillRect(screen, NULL, color_black); } int main(int argc, char *argv[]) { /* Initialize SDL */ if (SDL_Init(SDL_INIT_VIDEO) == -1) printf("Error: unable to initialize SDL: '%s'\n", SDL_GetError()); SDL_Surface *screen; screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_DEPTH, SDL_SWSURFACE); SDL_WM_SetCaption("Game", "Game"); SDL_Event event; int running = 1; Player player (20, 20); /****************************** Main Game Loop ******************************/ while (running) { SDL_PollEvent(&event); if (event.type == SDL_QUIT) { SDL_Quit(); return 0; } if (event.type == SDL_KEYDOWN) { if (event.key.keysym.sym == SDLK_ESCAPE) { SDL_Quit(); return 0; } } draw_player(screen, player.x, player.y); SDL_Flip(screen); clear_screen(screen); } return 0; } I made those 3 files, I think they're correct, but I don't know how to compile/link them with g++. Any idea? Thanks.
  14. munchor

    SDL and C++ Classes

    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.
  15. munchor

    Free movement and collision

    EnableKeyRepeat made it much better, if I don't succeed with a better way, I'll just use this, amazing! Do you want to remove the grid like movement mechanics altogether, or would you be happy to merely animate the player moving from tile to tile, such that motion appears smooth but when the player stops moving they finish exactly on a tile? I would like to keep the grid, on the one hand, because it makes collision so much simpler, and level design too (using a text editor if text files (like right now), or an image editor). So I would like the second option (animating the player), but if he keeps pressing the button, would it be possible to make him land on another tile? And how would I go about doing collision this way? This is how I'm going about right now: /* When the user presses the up key */ int wanted_position = map[player_y / TILE_SIZE - 1][player_x / TILE_SIZE]; if (not_block (wanted_position) player_y = player_y - TILE_SIZE;
  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. 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!