flat assembler
Message board for the users of flat assembler.

Index > Main > SDL for FASM

Author
Thread Post new topic Reply to topic
Christoph



Joined: 11 Jul 2007
Posts: 6
Christoph 11 Jul 2007, 10:14
Hello,

I am currently trying to use SDL (Simple Directmedia Layer) from FASM, but i have some Problems.

I have a complete import table (or whatever the name is), like this:

Code:
macro .sdl{
    section '.idata' import data readable writeable
        ;remove kernel and user from final version
        library \
            kernel,'KERNEL32.DLL',\
            user,'USER32.DLL',\
            sdl,'SDL.DLL'
        import kernel,\
            ExitProcess,'ExitProcess'
        import \
            user,\
            MessageBox,'MessageBoxA'
        import sdl,\
\;______________________________________________________________________________
\;General
            SDL_Init,'SDL_Init',\                                   ;dd SDL_Init(Uint32 flags);
            SDL_InitSubSystem,'SDL_InitSubSystem',\                 ;int SDL_InitSubSystem(Uint32 flags);
            SDL_QuitSubSystem,'SDL_QuitSubSystem',\                 ;void SDL_QuitSubSystem(Uint32 flags);
            SDL_Quit,'SDL_Quit',\                                   ;void SDL_Quit(void);
            SDL_WasInit,'int SDL_WasInit',\                         ;int SDL_WasInit(Uint32 flags);
            SDL_GetError,'SDL_GetError',\                           ;char *SDL_GetError(void);
            SDL_ClearError,'SDL_ClearError',\                       ;void SDL_ClearError(void);
            SDL_LoadObject,'SDL_LoadObject',\                       ;void *SDL_LoadObject(const char *sofile);
            SDL_LoadFunction,'SDL_LoadFunction',\                   ;void *SDL_LoadFunction(void *handle, const char *name);
            SDL_UnloadObject,'SDL_UnloadObject',\                   ;void SDL_UnloadObject(void *handle);
            SDL_putenv,'SDL_putenv',\                               ;int 
;
;more
;even more
;
    



And SDL defines a few Types like Uint8, Sint8, Uint16, Sint32... how can I use those? Is the following correct?

Code:
Uint32  fix dd ;do I need to use Uint32 fix dd ? (the ? for uninitialised data)?
Sint32  fix dd
Uint16  fix dw
Sint16  fix dw
Uint8   fix db
Sint8   fix db
int     fix dd
short   fix dw
long    fix dq
char    fix db
    



And SDL defines a lot of Structures, am i doing this right?:

Code:
struct SDL_version
    Uint8    major
    Uint8      minor
    Uint8      patch
ends

struct SDL_PixelFormat
    ;SDL_Palette *palette; ;wtf pointer to structure??? how to use in fasm?
    Uint8 BitsPerPixel
    Uint8       BytesPerPixel
    Uint8      Rloss
    Uint8      Gloss
    Uint8      Bloss
    Uint8      Aloss
    Uint8      Rshift
    Uint8     Gshift
    Uint8     Bshift
    Uint8     Ashift
    Uint32    Rmask
    Uint32     Gmask
    Uint32     Bmask
    Uint32     Amask
    Uint32     colorkey
    Uint8   alpha
ends


;the following is pretty hard, functionpointer?, unions
struct SDL_RWops
    ;int (*seek)(struct SDL_RWops *context, int offset, int whence);
    ;int (*read)(struct SDL_RWops *context, void *ptr, int size, int maxnum);
    ;int (*write)(struct SDL_RWops *context, const void *ptr, int size, int num);
    ;int (*close)(struct SDL_RWops *context);
    Uint32  type
    ;union
    ;    struct stdio
    ;        int autoclose
        ;FILE *fp;
    ;    ends
    ;    struct mem
        ;Uint8 *base;
        ;Uint8 *here;
        ;Uint8 *stop;
    ;    ends
    ;    struct unknown
        ;void *data1;
    ;    ends
    ;ends
ends


struct SDL_VideoInfo
    ;Uint32 hw_available:1    ;what is this? do all Uint32 use the same variable but different bits or what?
    ;Uint32 wm_available:1
    ;Uint32 blit_hw:1;
    ;Uint32 blit_hw_CC:1;
    ;Uint32 blit_hw_A:1;
    ;Uint32 blit_sw:1;
    ;Uint32 blit_sw_CC:1;
    ;Uint32 blit_sw_A:1;
    ;Uint32 blit_fill:1;
    Uint32  video_mem
    ;SDL_PixelFormat *vfmt;
    int     current_w
    int     current_h
ends

    



And to make everything a bit easier i would like to use the Constants from SDL. Does FASM support enumerations?

Code:
SDL_INIT_TIMER          =   0x00000001   ;does fasm support hexcodes
SDL_INIT_AUDIO          =   0x00000010
.
.
.
KMOD_CAPS               =       0x2000
KMOD_MODE               =       0x4000
KMOD_RESERVED           =       0x8000
KMOD_CTRL               =   KMOD_LCTRL|KMOD_RCTRL     ;can fasm do this?
KMOD_SHIFT              =   KMOD_LSHIFT|KMOD_RSHIFT
KMOD_ALT                =   KMOD_LALT|KMOD_RALT
KMOD_META               =   KMOD_LMETA|KMOD_RMETA

SDL_MAX_TRACKS          =           99
SDL_AUDIO_TRACK         =         0x00
SDL_DATA_TRACK          =         0x04

    



Is this static linking? If it is my code needs to be released under the LGPL.
I hope you can answer some of my questions.

-Christoph
Post 11 Jul 2007, 10:14
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 11 Jul 2007, 10:35
hi.

1. there is no reason to use "int8", "uint32" etc. in assembly. Just use plain "db", "dw", "dd", "dq". Also "fix" isn't really the best way to do this, "equ" should be enough.

2. structures look allright. Just again, you can use seach-and-replace to "db", "dw", "dd". Function pointer in 32bit assembly is simply dword.

3. I am afraid you will have to modify constants a little bit. "a | b" is in fasm "a or b".

4. If you are using DLL, it is a dynamic linking (DLL = dynamic link library).
Post 11 Jul 2007, 10:35
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Christoph



Joined: 11 Jul 2007
Posts: 6
Christoph 11 Jul 2007, 10:56
Thank you for your reply.

I planned to replace all those Unit, ints, chars with db, dw and so on, but i wasn't sure about the unsigned and signed ones.

I will replace the pointer with dw's.

So i have to replace KMOD_META = KMOD_LMETA|KMOD_RMETA with KMOD_META = KMOD_LMETA or KMOD_RMETA.
On the forum i have seen that hex-numbers in fasm look like this SDL_INIT_AUDIO = 00000010h instead of SDL_INIT_AUDIO = 0x00000010 can fasm do both? or only the first one?

Still i dont understand what Uint32 blit_hw:1; Uint32 blit_hw_CC:1; Uint32 blit_hw_A:1; is or how it could be implemented. Is this just one single dw that use some kind of masking?

Regards, Christoph
Post 11 Jul 2007, 10:56
View user's profile Send private message Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3175
Location: Denmark
f0dder 11 Jul 2007, 11:30
":n" inside a struct means this variable is only n bits wide.

signed vs. unsigned doesn't matter in assembly when declaring variables, but it does once you want to do calculations and conditional jumps...
Post 11 Jul 2007, 11:30
View user's profile Send private message Visit poster's website Reply with quote
Christoph



Joined: 11 Jul 2007
Posts: 6
Christoph 11 Jul 2007, 11:44
Thanks for your help.

So in Assembly i can ignore the :n and just use
Uint32 bla:1; -> dd bla ?

I have another question, double is in assembly dq right? when a function needs a double as parameter can i just use invoke func, somedoubleparam or do i need to use two dd's like invok func, 1sthalfofdouble, 2ndhalfdouble?

When i need a double in a struct can i use the following?

Code:
struct SDL_AudioCVT
    dd      needed
    dw      src_format
    dw      dest_format
    dq      rate_incr   ;double rate_incr
    dw      buf         ;Uint8 *buf;
    dd      len
    dd      len_cvt
    dd      len_mult
    dq      len_ratio   ;double len_ratio
    dw      filters     ;void (*filters[10])(struct SDL_AudioCVT *cvt, Uint16 format);
    dd      filter_index
ends
    
Post 11 Jul 2007, 11:44
View user's profile Send private message Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3175
Location: Denmark
f0dder 11 Jul 2007, 11:58
Christoph wrote:

So in Assembly i can ignore the :n and just use
Uint32 bla:1; -> dd bla ?

Nope, bitfields are packed, so you need to figure out what goes where, and then use the correct sequence of MOV, AND/OR, SHL/SHR etc. when accessing the fields... even more fun if you're dealing with signed bitfields.

Christoph wrote:

I have another question, double is in assembly dq right?

Yup. I personally prefer the longer names rather than "dx", but that's a matter of taste.

Christoph wrote:

when a function needs a double as parameter can i just use invoke func, somedoubleparam or do i need to use two dd's like invok func, 1sthalfofdouble, 2ndhalfdouble?

Depends on how smart the invoke macro is Smile, but internally it's two pushes, yes.
Post 11 Jul 2007, 11:58
View user's profile Send private message Visit poster's website Reply with quote
Christoph



Joined: 11 Jul 2007
Posts: 6
Christoph 11 Jul 2007, 12:23
Now i get it Smile
I found a more detailed describtion of the structure with the bitfield (commented), damn they are wasting 23 bits (future flags?).
Code:
struct SDL_VideoInfo
    dd      flags
    ;Uint32 hw_available:1;
    ;Uint32 wm_available:1;
    ;Uint32 UnusedBits1 :6;
    ;Uint32 UnusedBits2 :1;
    ;Uint32 blit_hw     :1;
    ;Uint32 blit_hw_CC  :1;
    ;Uint32 blit_hw_A   :1;
    ;Uint32 blit_sw     :1;
    ;Uint32 blit_sw_CC  :1;
    ;Uint32 blit_sw_A   :1;
    ;Uint32 blit_fill   :1;
    ;Uint32 UnusedBits3 :16;
    dd      video_mem
    dw      vfmt        ;SDL_PixelFormat *vfmt;
    dd      current_w
    dd      current_h
ends
    

here they wasted even more Smile
Code:
struct SDL_Overlay
    dd      format
    dd      w
    dd      h
    dd      planes
    dw      pitches     ;Uint16 *pitches;
    dw      pixels      ;Uint8 **pixels;
    dd      hw_overlay
    ;Uint32 hw_overlay:1;
    ;Uint32 UnusedBits:31;
ends
    



When i try to compile my source fasm says invalid macro argument
at line 283 (struct stdio). Whats wrong?

Code:
;typedef struct _iobuf FILE;
struct FILE
    dw      _ptr        ;char *_ptr;
    dd      _cnt
    db      base        ;char *_base;
    dd      _flag;
    dd      _file;
    dd      _charbuf;
    dd      _bufsiz;
    dw      _tmpfname   ;char *_tmpfname;
ends

struct SDL_RWops
    dw      seek        ;int (*seek)(struct SDL_RWops *context, int offset, int whence);
    dw      read        ;int (*read)(struct SDL_RWops *context, void *ptr, int size, int maxnum);
    dw      write       ;int (*write)(struct SDL_RWops *context, const void *ptr, int size, int num);
    dw      close       ;int (*close)(struct SDL_RWops *context);
    dd      type
    union
        struct stdio
            dd      autoclose
            dw      fp      ;FILE *fp
        ends
        struct mem
            dw      base    ;Uint8 *base;
            dw      here    ;Uint8 *here;
            dw      stop    ;Uint8 *stop;
        ends
        struct unknown
            dw      data1   ;void *data1;
        ends
    ends
ends
    


Thanks in advance.
Post 11 Jul 2007, 12:23
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 11 Jul 2007, 13:29
Because you have to define "fieldName type" not "type fieldName" like in C.
Post 11 Jul 2007, 13:29
View user's profile Send private message Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3175
Location: Denmark
f0dder 11 Jul 2007, 14:02
Also remember that the structs fields are probably dword-aligned rather than packed...
Post 11 Jul 2007, 14:02
View user's profile Send private message Visit poster's website Reply with quote
Christoph



Joined: 11 Jul 2007
Posts: 6
Christoph 14 Sep 2007, 12:45
Hello,

finally I have an almost complete import list for sdl and sdl_image.
But still I've got some problems.

When I try to import the function SDL_WM_IconifyWindow i get an error about if used, is there a workaround?
Code:
import sdl,\
...
SDL_WM_IconifyWindow,'SDL_WM_IconifyWindow',\
...
    


I don't get the Eventhandling working. How can I pass a pointer as argument to the SDL_PeepEvents function?
Code:
int SDL_PeepEvents(SDL_Event *events, int numevents, SDL_eventaction action, Uint32 mask);
???
invoke SDL_PeepEvents, event, 1, SDL_GETEVENT, SDL_ALLEVENTS
    

And how can I get the type of event.
SDL has defined one generic event but also defines multiple other events.
Code:
typedef union SDL_Event {
  Uint8 type;
 SDL_ActiveEvent active;
     SDL_KeyboardEvent key;
      SDL_MouseMotionEvent motion;
        SDL_MouseButtonEvent button;
        SDL_JoyAxisEvent jaxis;
     SDL_JoyBallEvent jball;
     SDL_JoyHatEvent jhat;
       SDL_JoyButtonEvent jbutton;
 SDL_ResizeEvent resize;
     SDL_ExposeEvent expose;
     SDL_QuitEvent quit;
 SDL_UserEvent user;
 SDL_SysWMEvent syswm;
} SDL_Event;

//these are examples of actual event structures but there are many more
/* The "quit requested" event */
typedef struct SDL_QuitEvent {
      Uint8 type;     /* SDL_QUIT */
} SDL_QuitEvent;

/* A user-defined event type */
typedef struct SDL_UserEvent {
       Uint8 type;     /* SDL_USEREVENT through SDL_NUMEVENTS-1 */
 int code;       /* User defined event code */
       void *data1;    /* User defined data pointer */
     void *data2;    /* User defined data pointer */
} SDL_UserEvent;
    


Currently I stopped my efford to get the sdl structs defined in fasm (atm to confusing for me) but I think I have all defines that are needed put into fasm (see libsdl.inc). Still I'm not sure if they are all correct, exspecially the shifted masks.
Code:
SDL_ALLEVENTS           = 0xFFFFFFFF
SDL_ACTIVEEVENTMASK     = 0x00000001 shl SDL_ACTIVEEVENT
SDL_KEYDOWNMASK             = 0x00000001 shl SDL_KEYDOWN
SDL_KEYUPMASK                   = 0x00000001 shl SDL_KEYUP
SDL_KEYEVENTMASK          = SDL_KEYDOWNMASK or SDL_KEYUPMASK
;does this work???
    


I attach my current sources and the libraries you need to run it, use them for whatever you want.

Hope you can give me a few tips.
Thanks in advance.


Description:
Download
Filename: sdl.zip
Filesize: 254.02 KB
Downloaded: 405 Time(s)

Post 14 Sep 2007, 12:45
View user's profile Send private message Reply with quote
0.1



Joined: 24 Jul 2007
Posts: 474
Location: India
0.1 17 Sep 2007, 06:26
Why are you taking so much of trouble?
Why are you writing it in assembly when your are using a library which is
written in C?
Why are you doing all this?

I once tried (2-3 years back) to use SDL + assembly. I was using gas
so I did not need any import for functions. I did not define any structs.
I was writing a game so along with my game I declared the needed constants, structs (just declared constants with size and offsets of structs) in header files one by one.
And my game (a snake game. duh!) was working fine!
But it was so tiresome! You should at least use C.
Post 17 Sep 2007, 06:26
View user's profile Send private message Reply with quote
Christoph



Joined: 11 Jul 2007
Posts: 6
Christoph 17 Sep 2007, 08:02
Well, I already used SDL with C/C++, but I want to learn assembly a bit more.
Currently I'm not planing doing a whole game in asm, I just want to experiment a bit.
I use SDL because once I get it running it is pretty easy to use and it's a nice library.

If I really wanted to make a game I would not use SDL and/or asm, I was propably going for C++ and Nebula 2 or something like that.

Yes I know it's troublesome and timeconsuming but I'm a student and students have a lot time Smile
Post 17 Sep 2007, 08:02
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
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
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.