FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Creating a wrapper between SDL Key values

 
Post new topic   Reply to topic     Forum Index -> Derelict
View previous topic :: View next topic  
Author Message
RedShft



Joined: 15 Feb 2012
Posts: 12

PostPosted: Wed Feb 29, 2012 9:48 pm    Post subject: Creating a wrapper between SDL Key values Reply with quote

I'm trying to create a wrapper between SDL key values by creating my own. Right now I have a class that deals with SDL directly, and then I'm writing my wrapper on top of that. However, when I press space (The key i'm using to test) nothing happens.

I first tried setting my Space bar key value to the SDL's but no result.

What am I doing wrong?

This is what I have so far:

--SDLInput---
Code:

import derelict.sdl.sdl;
import SDLApp;

class SDLInput
{
private:
   SDLApp sdlapp;
   SDL_Event event;
public:
   this()
   {
      event = sdlapp.event;
   }
   bool KeyPressed(SDLKey key)
   {
      if(event.type == SDL_KEYDOWN && event.key.keysym.sym == key)
         return true;
      else
         return false;
   }

   bool KeyReleased(SDLKey key)
   {
      if(event.type == SDL_KEYUP && event.key.keysym.sym == key)
         return true;
      else
         return false;
   }
}



---DPInput---
Code:

import SDLInput;
import derelict.sdl.sdl; /*This is just temporary to test if I can assign enum values to each other*/

class DPInput
{
   SDLInput sdlinput;

   enum DPKey
   {
      DPK_SPACE = SDLK_SPACE
   }
   this()
   {
      sdlinput = new SDLInput;
   }
   bool KeyPressed(DPKey key)
   {
      return sdlinput.KeyPressed(key);
   }
}
Back to top
View user's profile Send private message
aldacron



Joined: 05 May 2004
Posts: 1322
Location: Seoul, South Korea

PostPosted: Wed Feb 29, 2012 11:02 pm    Post subject: Re: Creating a wrapper between SDL Key values Reply with quote

RedShft wrote:

What am I doing wrong?


My guess is that the problem is here:
Quote:

--SDLInput---
Code:

   this()
   {
      event = sdlapp.event;
   }


SDL_event is a struct, which means it is passed around by value. The assignment you have there is making a copy of sdlapp.event. You didn't post your event loop, but assuming the SDLApp class updates its event member properly, SDLInput will *never* see it, because it has a copy of the original and not a pointer or reference to it.

With your current design, you should have something like this:

Code:


// in the SDLApp class
// The private event instance
SDL_Event event;

// The event property accessor
SDL_Event* event() @property
{
    return &event;
}

// In the SDLInput class
// SDL_Event event; <--- wrong!
SDL_Event* event;


Now, SDLInput will always have access to the original event object.

But another issue is, where does the sdlapp member get initialized? You aren't passing it in the constructor. Did you just leave that part out in the code you posted?

Aside from that, there's a lot that could be improved with this. For example, SDLInput doesn't need it's own event member, since it already has access to sdlapp.event. So one easy improvement would be:

Code:

class SDLInput
{
private:
   SDLApp sdlapp;
public:
   this(SDLApp app )
   {
        sdlapp = app;
   }
   bool KeyPressed(SDLKey key)
   {
      auto event = sdlapp.event;
      if(event.type == SDL_KEYDOWN && event.key.keysym.sym == key)
         return true;
      else
         return false;
   }

   bool KeyReleased(SDLKey key)
   {
      auto event = sdlapp.event;
      if(event.type == SDL_KEYUP && event.key.keysym.sym == key)
         return true;
      else
         return false;
   }
}


Another easy one is that you might also consider making SDLInput a struct rather than a class if you don't need any specific class features (like virtual functions, interfaces, and such).
_________________
The One With D | The One With Aldacron | D Bits
Back to top
View user's profile Send private message Send e-mail
RedShft



Joined: 15 Feb 2012
Posts: 12

PostPosted: Wed Feb 29, 2012 11:23 pm    Post subject: Reply with quote

Thank you for the swift reply.

Your guess was absolutely correct. Much appreciated.

As far as sdlapp in SDLInput goes, the reason it wasn't initialized is because I set event to be static:

Code:

static SDL_Event event;


I'm not sure if that was appropriate, but it seemed like a good idea at the time.

Right now with the correction of the KeyPressed and KeyReleased functions in SDLInput I get text in the console. So far so good.

I'm fairly new to D, so thank you for your help!
Back to top
View user's profile Send private message
aldacron



Joined: 05 May 2004
Posts: 1322
Location: Seoul, South Korea

PostPosted: Thu Mar 01, 2012 12:11 am    Post subject: Reply with quote

RedShft wrote:

As far as sdlapp in SDLInput goes, the reason it wasn't initialized is because I set event to be static:

Code:

static SDL_Event event;


I'm not sure if that was appropriate, but it seemed like a good idea at the time.


If you are going that way, then you don't need an instance of SDLApp inside SDLInput. You can access the event member just using the class name, so SDLInput becomes:

Code:

import SDLApp;

class SDLInput
{
public:
   bool KeyPressed(SDLKey key)
   {
      auto event = SDLApp.event;
      if(event.type == SDL_KEYDOWN && event.key.keysym.sym == key)
         return true;
      else
         return false;
   }

   bool KeyReleased(SDLKey key)
   {
      auto event = SDLApp.event;
      if(event.type == SDL_KEYUP && event.key.keysym.sym == key)
         return true;
      else
         return false;
   }
}

_________________
The One With D | The One With Aldacron | D Bits
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> Derelict All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group