flat assembler
Message board for the users of flat assembler.

Index > Tutorials and Examples > Simple game engine with SDL2

Author
Thread Post new topic Reply to topic
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8367
Location: Kraków, Poland
Tomasz Grysztar 22 Apr 2018, 10:46
This is a very simple groundwork for a game that I've been making a few years ago with my friends, purely for fun. As it has been abandoned then, they gave me a permission to share it and now I release this code as a public domain.

It was originally created with fasm 1 and later converted to fasmg. But a few minor tweaks should suffice to make it assemble with fasm 1 once more. Assembling it with fasmg requires the fasm 1 compatibility macros.

It uses 32-bit SDL2 library. For Windows the required DLLs are included, on Linux 32-bit version of libSDL2 needs to be installed with all components.

You may notice that it shares some similarities with my much older engine for DOS, except this one has been abandoned at even earlier stage. There are no "lemmings" to decimate here, they were never added. The player is left with nothing more than punching and kicking the air.

To start the game, a .DEF file needs to be provided as an argument from command line or by dropping the file onto the program. There are two .DEF files included.


Description: A simple game engine made with SDL2, assembled with fasmg
Download
Filename: prahorda.zip
Filesize: 5.96 MB
Downloaded: 2284 Time(s)

Post 22 Apr 2018, 10:46
View user's profile Send private message Visit poster's website Reply with quote
yeohhs



Joined: 19 Jan 2004
Posts: 195
Location: N 5.43564° E 100.3091°
yeohhs 22 Apr 2018, 12:48
Very Happy Nice! Thank you very much.
Post 22 Apr 2018, 12:48
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8367
Location: Kraków, Poland
Tomasz Grysztar 24 Apr 2018, 14:05
For those not eager to read the entire source to find out what the keyboard commands are, basic instructions below.

Left/right arrow - move around
Up arrow - jump
P - punch, K - kick (corresponding animations are defined only in PARTY.DEF)
Enter - switch full screen / windowed
Space - turn on "party mode" (blinking effect visible only on transparent background)

There are also some additional commands to further test the graphics engine:
W/S/A/D - move the viewport around
-/= - resize the viewport

The .DEF files are a simple text, you can easily edit them or create additional ones. Background/terrain should be provided in form of a PNG with alpha channel, the boundaries of terrain to walk on are taken from alpha.

The planes are defined in .DEF file with a "plan" command, followed by the name of the file and then either "=" to define the terrain plane or a parallax proportion, like "1:2" for a background parallax and "2:1" for a foreground parallax. The planes are drawn in the order in which they are defined in file, so the backgrounds need to be defined first, then the terrain, and then foregrounds. If there is another number after the parallax definition, it defines the speed at which the plane is moving relative to the terrain.

The definitions of character animations contain sets of parameters that look like:
X Y WxH[N/M] (DX DY)
X and Y are coordinates of the first frame (in the image that contains all of them), W and H define dimensions of a single frame (all frames need to be of the same size and adjacent to each other), N is the number of frames, M is at how many frames per second the animation plays (so the N/M fraction does at the same time define how long the animation is in seconds). DX and DY allow to offset the animation relative to the actual coordinates of the animated character.

There are several additional parameters defined as assembly-time constants on top of CODE.INC, like the strength of jump etc. One could easily change them into run-time parameters (perhaps read from .DEF) if needed.
Post 24 Apr 2018, 14:05
View user's profile Send private message Visit poster's website Reply with quote
Picnic



Joined: 05 May 2007
Posts: 1405
Location: Piraeus, Greece
Picnic 17 Aug 2018, 08:26
Nice to sharing the source Tomasz.

The road.def level looks like the 2nd level of the game Bad Dudes Vs. DragonNinja, very cool Smile

Image

Image
Post 17 Aug 2018, 08:26
View user's profile Send private message Visit poster's website Reply with quote
TheRaven



Joined: 22 Apr 2008
Posts: 91
Location: U.S.A.
TheRaven 09 Mar 2019, 08:06
I do remember Bad Dudes --played many Data East and SNK rumblers & shmups.
Didn't think T.G. had the Fist of the North Star in him -nice.
Post 09 Mar 2019, 08:06
View user's profile Send private message Reply with quote
Mat Quasar



Joined: 15 Dec 2024
Posts: 87
Mat Quasar 07 Feb 2025, 12:36
Tomasz Grysztar wrote:
It was originally created with fasm 1 and later converted to fasmg. But a few minor tweaks should suffice to make it assemble with fasm 1 once more.


Hi, I am using the SDL2 files and Tomasz's game as reference to do my graphical version of space invader game.

But I am using fasm 1, and have near to zero knowledge in macro.
May I know how to make the following line works with fasm 1?

Code:
struc SDL_DEFINE_PIXELFORMAT type,order,layout,bits,bytes
    . = 1 shl 28 or (type) shl 24 or (order) shl 20 or (layout) shl 16 or (bits) shl 8 or (bytes)
end struc
    


FASMW.EXE wrote:
Error: unexpected character


Please help.... I may need more help as I go through the source code. Thanks! Cool
Post 07 Feb 2025, 12:36
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 1066
Location: Russia
macomics 07 Feb 2025, 13:20
It declares a dword with the following content 0001ttttoooollllyyyyyyyyxxxxxxxxb t - type, o - order, l - layout, y - bits, x - bytes b - binary suffix for number
Code:
; fasm1
struc SDL_DEFINE_PIXELFORMAT type=0,order=0,layout=0,bits=0,bytes=0 { label . dword
  .bytes db bytes
  .bits db bits
  .layout_and_order db ((order) shl 4) or (layout)
  .type db 0x10 or (type)
}    


Last edited by macomics on 07 Feb 2025, 14:09; edited 1 time in total
Post 07 Feb 2025, 13:20
View user's profile Send private message Reply with quote
Mat Quasar



Joined: 15 Dec 2024
Posts: 87
Mat Quasar 07 Feb 2025, 13:36
macomics wrote:
It declares a dword with the following content 0001ttttoooollllyyyyyyyyxxxxxxxxb t - type, o - order, l - layout, y - bits, x - bytes b - binary suffix for number
Code:
; fasm1
struc SLD_DEFINE_PIXELFORMAT type=0,order=0,layout=0,bits=0,bytes=0 { label . dword
  .bytes db bytes
  .bits db bits
  .layout_and_order db ((order) shl 4) or (layout)
  .type db 0x10 or (type)
}    


Thanks @macomics, now I have another error, not sure if related or not related to the macro above:

Code:
SDL_PIXELFORMAT_INDEX1LSB \
    SDL_DEFINE_PIXELFORMAT SDL_PIXELTYPE_INDEX1, SDL_BITMAPORDER_4321, 0, \
                           1, 0       


Error in the screenshot, please see.


Description: Another error
Filesize: 47.74 KB
Viewed: 1159 Time(s)

sdl2.PNG


Post 07 Feb 2025, 13:36
View user's profile Send private message Reply with quote
Mat Quasar



Joined: 15 Dec 2024
Posts: 87
Mat Quasar 07 Feb 2025, 13:56
Please ignore the post above. Problem solved....because of small typo:
Code:
SLD_DEFINE_PIXELFORMAT
    


...should be SDL_XXXXX.

Thank you , @macomics!
Post 07 Feb 2025, 13:56
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 1066
Location: Russia
macomics 07 Feb 2025, 14:17
Code:
; fasm1
struc SDL_DEFINE_PIXELFORMAT type=0,order=0,layout=0,bits=0,bytes=0 {
  . = (1 shl 28) or (((type) and 15) shl 24) or (((order) and 15) shl 20) or \
    (((layout) and 15) shl 16) or (((bits) and 255) shl 8) or \
      ((bytes) and 255)
}    
This is a more correct declaration. The original one does not specify a dword, but simply defines a constant.
Post 07 Feb 2025, 14:17
View user's profile Send private message Reply with quote
Mat Quasar



Joined: 15 Dec 2024
Posts: 87
Mat Quasar 07 Feb 2025, 14:31
macomics wrote:
Code:
; fasm1
struc SDL_DEFINE_PIXELFORMAT type=0,order=0,layout=0,bits=0,bytes=0 {
  . = (1 shl 28) or (((type) and 15) shl 24) or (((order) and 15) shl 20) or \
    (((layout) and 15) shl 16) or (((bits) and 255) shl 8) or \
      ((bytes) and 255)
}    
This is a more correct declaration. The original one does not specify a dword, but simply defines a constant.


Thanks for your improvement. I am not able to run Tomasz's game yet on fasm 1, but compile error-free on my new game code (with your modified macro) - I may eventually run into fasmg-to-fasm1 error again as I add more code from Tomasz's example to my game code.
Post 07 Feb 2025, 14:31
View user's profile Send private message Reply with quote
sylware



Joined: 23 Oct 2020
Posts: 477
Location: Marseille/France
sylware 08 Feb 2025, 15:58
wowowow, guys libSDL3 is getting close to release, you better jump on the wagon because I can tell you, that's not going to be a small API breakage. (dota2 and cs2 have been using libSDL3 for more than a year)
Post 08 Feb 2025, 15:58
View user's profile Send private message Reply with quote
Ali.Z



Joined: 08 Jan 2018
Posts: 772
Ali.Z 08 Feb 2025, 22:12
sylware wrote:
(dota2 and cs2 have been using libSDL3 for more than a year)


valve has their own internal version of sdl, and it is different.

_________________
Asm For Wise Humans
Post 08 Feb 2025, 22:12
View user's profile Send private message Reply with quote
sylware



Joined: 23 Oct 2020
Posts: 477
Location: Marseille/France
sylware 09 Feb 2025, 18:13
dota2 and cs2 libSDL3 is the official libSDL3, with some lag.

I know because I did fix linux alsa support in libSDL3 (severely broken).

Icculus said he will do the merge.
Post 09 Feb 2025, 18:13
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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.