PDA

View Full Version : Help SDL behaves strangely...



asdfarkangel
27-10-2010, 06:55 PM
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:D 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.

code_glitch
27-10-2010, 08:52 PM
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.

WILL
27-10-2010, 08:59 PM
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:D 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...


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


// --- 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.

asdfarkangel
27-10-2010, 09:19 PM
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. :D 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:D)

asdfarkangel
27-10-2010, 09:33 PM
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:


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.

code_glitch
27-10-2010, 09:42 PM
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.

asdfarkangel
27-10-2010, 09:53 PM
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:D 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.

WILL
28-10-2010, 03:28 AM
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.

asdfarkangel
28-10-2010, 11:03 AM
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.

code_glitch
28-10-2010, 11:36 AM
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.

asdfarkangel
28-10-2010, 12:14 PM
it's not long at all:D and thanks for it. I used to use Turbo Pascal at the very beginning of my "pascal carreer" but that just sucked in several ways so when I had started this again(about a month ago) my friend suggested me Free Pascal. And not long after that I found this site where I also found out that this SDL is compatible with Free Pascal so that seemed a comfortable way to go. I will look at your Prometheus game but now I am going to have the most useless class ever:D "Learning Methods" (in a Medical University) Fortunately this is the last class in the semester(and at all since this course is just 1 semester long) and I get 1 credit for it so now for the following 6 years I only have to get 359 more to get my degree:D.

code_glitch
28-10-2010, 12:43 PM
Well, good luck with your class. And the purpose of Prometheus was to make video, sound, event handling and all that even simpler than sdl without limiting flexibility too much. On the whole it's worked so far but I'm overhauling it (its on the main thread here: http://www.pascalgamedevelopment.com/forumdisplay.php?62-Prometheus-Game-Library ) if you're interested in it. The new updates I'm putting forward to it are the first drafts of audio, the new event handler data types and improving performance of the video unit by switching from code that uses SDL to hardware accelerated OpenGl, its due out later this week/early next week, but again its a work in progress and I've only just started writing the documentation. So I wouldn't recommend basing your entire game on it, just grab what you need from it since the code isnt mature yet.

Well, better get cracking. First lunch and then onto some code. Hey, its my holiday, whod've guessed it? I get more work done on holidays in general though since I dont have stupid GCSE work...
cya,
code_glitch.

asdfarkangel
28-10-2010, 08:56 PM
Oh well thanks. I will really look at what it is about. By the way. Since this problem has been solved the thread can be closed I think.