Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Help SDL behaves strangely...

  1. #1

    Help SDL behaves strangely...

    Hi. First I describe what I want to do and then why I am unable to that. So I started a torpedo(Battleships) game. And I use SDL to get pictures on the screen. Now I want to use the mouse to shoot. This would be like when the user presses the left mouse button. I get the position of the cursor and calculate the coordinates from it. Easy right? Not for me First problem: How do I make the program to basicly do nothing just wait until the user presses the mouse button? I tried simple loops but it didn't really care about I had already pressed the button it just stayed there in a never ending loop. Second is SDL's behaviour is very strange. If I just load the picture and do not do anything else, the cursor changes to the well-known hour glass and it doesn't seem to respond to anything I do in the SDL window. Even if I did nothing just drew the pictures. What may I be doing wrong? I tried to do things similar to the tutorial but since I do not need those rectangles this time I set those as NIL as they should be.
    Last edited by asdfarkangel; 27-10-2010 at 07:01 PM.

  2. #2
    PGD Staff code_glitch's Avatar
    Join Date
    Oct 2009
    Location
    UK (England, the bigger bit)
    Posts
    933
    Blog Entries
    45
    you need to sdl_pollevent once every so often (around 40-100 miliseconds) to get the event queue from windows. That will make your hourglass go away. As for waiting for user input a simple while loop that checks for events until a mousedown event should suffice.
    I once tried to change the world. But they wouldn't give me the source code. Damned evil cunning.

  3. #3
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25
    Quote Originally Posted by asdfarkangel View Post
    Now I want to use the mouse to shoot. This would be like when the user presses the left mouse button. I get the position of the cursor and calculate the coordinates from it. Easy right? Not for me First problem: How do I make the program to basicly do nothing just wait until the user presses the mouse button? I tried simple loops but it didn't really care about I had already pressed the button it just stayed there in a never ending loop.
    Ok, first lets address your mouse issue. You want to make sure that you are using local variables to store your mouse state and check those for updates. You should have something like the following...

    Code:
    var
      MouseX, MouseY: Integer; // mouse screen location
      MouseBtnLeft, MouseBtnMiddle, MouseBtnRight: Boolean; // true = Button pressed
    With these you can store your mouse values when you run the function to check your mouse state update. You want to update these values EACH TIME your game goes through your main game loop. And it would also be advisable to do all of your user input at the very beginning of your main loop as well. Now you have to make sure that each time you check for your mouse state, you do so AFTER you update your mouse state variables. If you don't then you'll be reading the old state values and your input will act funny. This part is important.

    You game's input code in your main loop should something roughtly like this...

    Code:
    // --- begin user input ---
    {Update Mouse State variables}
    
    //  Left Mouse Button
    if (Left mouse button pressed) then
    begin
         if (mouse where you need it) then
            {Do whatever you need your game to do...}
    end;
    //  Right Mouse Button
    if (Right mouse button pressed) then
    begin
         if (mouse where you need it) then
            {Do whatever you need your game to do...}
    end;
    // --- end user input ---
    Hopefully that helps. I unfortunately had to rewrite it so I hope I didn't miss anything critical. So for the graphics I'll pop bakc and give it another read over and see if I can help out.
    Jason McMillen
    Pascal Game Development
    Co-Founder





  4. #4
    Thanks I didn't know that sdl_pollevent is so important. That must be the error here WILL thanks for that info too. I was trying something similar I just coulnd't experience how what I was trying was working and debug it because of this freeze thing. It's just a small game and it gave me more headache in a few days than most of my previous programs overall.(Except for one... OFF:
    That was some kind of record handler. Read in some variables and stored them in binary files and I wrote a search engine for it. One of the parameters of the record was an array of strings called "tags". Now I've tried like when searching it just simply asks for a string which looks like this:"tag1 tag2 tag3 tag4" of course this is one string and I had to do some tricks to fraction the strings at the spaces and get them into another array and with a double for loop I just compare all to all. For some reason however the results were windings like characters so they never made a match. I just solved that by first asking for number of tags and then read them in one by one but I was upset because the other way would have been more cool and I already wrote functions like it counts how many words a string consits of and others)

  5. #5
    Oh I forgot. To make this clear and precede any further questions about this.WILL so you are saying my program in the end should look something like this:
    Code:
    Program torpedo;
    uses sdl;
    {-type definitions
    -variable definitions
    -input procedure with local mouse coordinate variables
    -other game procedures such as drawing the boards and other sprites, playing the sounds
    -calculating the shots etc...}
    BEGIN
    //-graphics, sound and other initialization.
    While loopconditions=true do
    begin
    {-input procedure
     -other procedures, including looping music, refreshing graphics and gameplay status}
    if somecondition then loopconditions:=false;
    end;
    {-freeing current sdl surfaces except for screen
    -drawing victory/lose screen
    -waiting for keypress for example
    -shutting down SDL}
    end.
    Last edited by asdfarkangel; 27-10-2010 at 09:38 PM.

  6. #6
    PGD Staff code_glitch's Avatar
    Join Date
    Oct 2009
    Location
    UK (England, the bigger bit)
    Posts
    933
    Blog Entries
    45
    more or less. you first initialize and do startup stuff then while SDL_QUIT event is no true you do the main loop that process variables, gets events, plays music and draws stuff each cycle. when the aplication recieves the quit signal you then shut it down. sorry for being vague, but its late and I am soooo tired... But on the whole, your code you posted is quite correct but where it says somecondition, that would be the quit event.

    cheers.
    I once tried to change the world. But they wouldn't give me the source code. Damned evil cunning.

  7. #7
    Hmm and then I should put the events that happen at the end of the game (not when SDL quits, I could still need it for a nice scoreboard or something like that) inside the main loop in a conditional and check if the game has ended each time the game loops? Still sounds logical and it also helps to avoid some unexpected errors I think. So thanks again. You guys help me a lot I'm very grateful for that. This way this game will be finished in no time and I can start another and then another and each one more difficult so I might get better and have less basic questions like this It's just both SDL and OOP are yet new for me. Object orientation is half-new(there was something that could almost be called similar to object orientation in Game Maker) Or after this game I might study a bit of Delphi it seems useful. Maybe more useful than FPC.

  8. #8
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25
    You will eventually want to make a game state/mode variable to keep track of what state your game is currently in too. ie Intro screens, main menu, in-game play mode, in-game pause mode, end game credits and so on. You'll have to determine for yourself what you need based on how you organize your code. But do organize it so that it's easy to come back to later. It make later development and updating older projects so much more easier when you took the time to comment and keep your code orderly.
    Jason McMillen
    Pascal Game Development
    Co-Founder





  9. #9
    Oh I wonder why I haven't thought about that since my complicated games were structured like that in Game Maker too except for that I didn't make state variables but jumped to different rooms it was easier that way. Now it's the same concept just a bit different method. And I always keep my code in order. Mostly it's because me and my friend occasionally show each other our programs and we must write them in a way easy to understand for the other too. So usually it is full of comments and I go one tab inside after every begin or repeat.

  10. #10
    PGD Staff code_glitch's Avatar
    Join Date
    Oct 2009
    Location
    UK (England, the bigger bit)
    Posts
    933
    Blog Entries
    45
    Game state is usually quite a good way to go for complicated games... I found that making a game without procedures or functions (only one big main begin end. statement) with no records or anything is impossible. I got around 2,260 lines in and then found sustainability was impossible. And that without graphics since it was meant to be like nethack (hehe).

    Reading this thread though, makes me want to step back out there and get to game coding once again though. Maybe more work on the libraries I made for RPGs? Or perhaps on the one I started for Mario style platformers? Oh, and of course I'll keep throwing more lines at prometheus. Although now I think of it, you aren't Lazarus or Delphi are you? From those that use it I hear it's really great. As for my Prometheus suggestion, although its quite new, its event handling is (I find) quite useful. I've just started writing the documentation for it, so you would probably be able to either use the full core unit, or just svn the source code and take what you need. Thats what its there for after all.

    Sorry for the long post, but hey, hope it helps.
    code_glitch.
    I once tried to change the world. But they wouldn't give me the source code. Damned evil cunning.

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •