Jump to content
  • Advertisement
Sign in to follow this  
Drakkcon

Grabbing input in SDL.net

This topic is 4879 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm using sdl.net with C#, and I can't get my pong paddles to move up and down. The answer is probably obvious, but I've stared at the code too long: main.cs
#region Using directives

using System;
using System.Drawing;
using System.Collections.Generic;
using SdlDotNet;

#endregion

namespace RefTest
{
    public class MainSource
    {
        private bool IsRunning;

        private Video video = Video.Instance;
        private WindowManager wm = WindowManager.Instance;
        private Events events = Events.Instance;
        private SdlCore AppCore = SdlCore.Instance;

        private const int SCREEN_WIDTH = 640;
        private const int SCREEN_HEIGHT = 480;
        private const int SCREEN_DEPTH = 16;

        private Paddle LeftPaddle;
        private Paddle RightPaddle;

        MainSource()
        {
            IsRunning = true;
            wm.Caption = "Aerial Pong";
        }

        private void KeyboardDown(object sender, KeyboardEventArgs e)
        {
            if (e.Key == Key.Escape ||
                e.Key == Key.Q)
            {
                IsRunning = false;
            }

            if (e.Key == Key.UpArrow)
            {
                LeftPaddle.GoUp();
            }

            if (e.Key == Key.DownArrow)
            {
                LeftPaddle.GoDown();
            }
        }

        private void Quit(object sender, EventArgs e)
        {
            IsRunning = false;
        }

        public void RunProgram()
        {
            
            Rectangle rect = new Rectangle(0, 0, 0, 0);
            Surface screen = video.SetVideoModeWindow(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_DEPTH, true);

            events.KeyboardDown += new KeyboardEventHandler(this.KeyboardDown);
            events.Quit += new QuitEventHandler(this.Quit);

            LeftPaddle = new Paddle(0, screen);
            RightPaddle = new Paddle(SCREEN_WIDTH - 15, screen);
            LeftPaddle.Draw();
            RightPaddle.Draw();


            while (IsRunning)
            {
                while (events.PollAndDelegate()) { } //keep pushing events until there aren't any
                
            }

        }

        static void Main(string[] args)
        {
            MainSource MainSource = new MainSource();
            MainSource.RunProgram();

        }
    }
}
#region Using directives

using System;
using System.Drawing;
using System.Collections.Generic;
using SdlDotNet;

#endregion

namespace RefTest
{
    public class MainSource
    {
        private bool IsRunning;

        private Video video = Video.Instance;
        private WindowManager wm = WindowManager.Instance;
        private Events events = Events.Instance;
        private SdlCore AppCore = SdlCore.Instance;

        private const int SCREEN_WIDTH = 640;
        private const int SCREEN_HEIGHT = 480;
        private const int SCREEN_DEPTH = 16;

        private Paddle LeftPaddle;
        private Paddle RightPaddle;

        MainSource()
        {
            IsRunning = true;
            wm.Caption = "Aerial Pong";
        }

        private void KeyboardDown(object sender, KeyboardEventArgs e)
        {
            if (e.Key == Key.Escape ||
                e.Key == Key.Q)
            {
                IsRunning = false;
            }

            if (e.Key == Key.UpArrow)
            {
                LeftPaddle.GoUp();
            }

            if (e.Key == Key.DownArrow)
            {
                LeftPaddle.GoDown();
            }
        }

        private void Quit(object sender, EventArgs e)
        {
            IsRunning = false;
        }

        public void RunProgram()
        {
            
            Rectangle rect = new Rectangle(0, 0, 0, 0);
            Surface screen = video.SetVideoModeWindow(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_DEPTH, true);

            events.KeyboardDown += new KeyboardEventHandler(this.KeyboardDown);
            events.Quit += new QuitEventHandler(this.Quit);

            LeftPaddle = new Paddle(0, screen);
            RightPaddle = new Paddle(SCREEN_WIDTH - 15, screen);
            LeftPaddle.Draw();
            RightPaddle.Draw();


            while (IsRunning)
            {
                while (events.PollAndDelegate()) { } //keep pushing events until there aren't any
                
            }

        }

        static void Main(string[] args)
        {
            MainSource MainSource = new MainSource();
            MainSource.RunProgram();

        }
    }
}


Paddle.cs
#region Using directives

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using SdlDotNet;

#endregion

namespace RefTest
{
    public class Paddle
    {
        private int Speed;
        private Rectangle DimensionRect;
        Surface DestSurface;
        SdlDotNet.Image ImageToUse;
        int MaxY, MinY;

        public int GetWidth()
        {
            return DimensionRect.Width;
        }

        public int GetHeight()
        {
            return DimensionRect.Height;
        }

        public Paddle(int InitialPositionX, SdlDotNet.Surface DestSurf)
        {
            DimensionRect.Width = 15;
            DimensionRect.Height = 80;
            DimensionRect.X = InitialPositionX;
            DimensionRect.Y = 200;

            MaxY = 480 - DimensionRect.Height;
            MinY = 0;

            DestSurface = DestSurf;

            Speed = 2;

            ImageToUse = new SdlDotNet.Image("Paddle.bmp");
        }

        public void GoDown()
        {
            if (DimensionRect.Y >= MaxY)
            {
                DimensionRect.Y += Speed;
                Draw();
            }
        }

        public void GoUp()
        {
            if (DimensionRect.Y <= MinY)
            {
                DimensionRect.Y -= Speed;
                Draw();
            }
        }

        public void Draw()
        {
            ImageToUse.Draw(DestSurface, DimensionRect);
        }
    }
}


Share this post


Link to post
Share on other sites
Advertisement
Hi again ;)

Are you sure your keyboard-handlers aren't getting called? The event should fire if your windows has focus. However, you don't seem to Flip() the doublebuffered surface anywhere, which must be done to show the buffer you have rendered to.

You'll probably want to structure your code a bit differently. Instead of rendering directly from your keyboard event, try creating a timer that refreshes the screen every frame (and handles any non-player changes), and just change object positions from your input handlers. Something like this:


System.Threading.Timer timer = new System.Threading.Timer( new System.Threading.TimerCallback(Update), null, 0, 1000/60 );

public void Update ( )
{
try
{
if( Screen.MustLock )
Screen.Lock( );

// Do rendering

if( Screen.MustLock )
Screen.Unlock( );

Screen.Flip();
}
catch( SurfaceLostException ) { }
catch( Exception ) { }
}



Share this post


Link to post
Share on other sites
Yea, I'm flipping the surface now, but it still doesn't work. Oddly enough the escape key is captured but the up and down arrows aren't. Is it a problem with the paddle?

Share this post


Link to post
Share on other sites
its me again [razz]

i'm not sure where you got the idea to use Keys.UpArrow and .DownArrow, but vc# express doesn't let me use them. now, i'm using .net 2.0, but i seem to remember trying that and it not working. i just wrote a quick test that involved the KeyDown event altering a PictureBox i dropped into a quick test project, and here's what worked:


private void Form1_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Up:
pictureBox1.Top--;
break;
case Keys.Down:
pictureBox1.Top++;
break;
}
}



post some screenshots! [wink]

[edit: err - is all that stuff sdl junk? check your constants for Up and UpArrow to see if both are there, and try both.]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!