Author Topic: SDL 2 Hello World NASM ALINK Win32 API  (Read 10459 times)

Offline encryptor256

  • Full Member
  • **
  • Posts: 250
  • Country: lv
  • Win64 .
    • On Youtube: encryptor256
SDL 2 Hello World NASM ALINK Win32 API
« on: October 02, 2013, 01:34:53 PM »
Hello!

; *************************************************
; *************************************************
; *************************************************
; This is: SDL 2 Hello World NASM ALINK Win32 API.
; Author: J.K. Encryptor256.
; Date: October 02, year of 2013.
; *************************************************
; Using: Netwide assembler and ALINK.
; Nasm: Produces OBJ file.
; ALINK: Produces EXE file, from OBJ file. (Just 3,148 bytes of EXE)
; *************************************************
; Compile:
; Nasm: "nasm -fobj program.asm".
; Alink: "alink -c -oPE -subsys windows program.obj".
; *************************************************
; Intro:
; I found SDL 2 tutorial on the web: twinklebeardev.blogspot.com.
; So i learned it, created in my own way,
; translated into NASM assembly.
; Also, SDL 2 function description i found on web: wiki.libsdl.org.
; *************************************************
; +Program Requires:
; 1. Bitmap: hello.bmp
; 2. Library: sdl2.dll
; *************************************************
; +Description:
; Program loads hello.bmp,
; displays it on the application owned screen.
; Program terminates upon keypress or ordinary quit.
; *************************************************
; *************************************************
; *************************************************

.LET_CODE_BEGIN:

Code: [Select]
; ############################################
; # Create macro, who imports functions

%macro importfunc 2
        extern %2
        import %2 %1
%endmacro

; # Tell compiler what cpu type to use
cpu 386

; # Tell compiler to generate 32 bit code
bits 32

; # Define some constants

        ; SDL constants ------------------------------------------------

                SDL_INIT_TIMER equ 0x00000001 ; timer subsystem
                SDL_INIT_AUDIO equ 0x00000010 ; audio subsystem
                SDL_INIT_VIDEO equ 0x00000020 ; video subsystem
                SDL_INIT_JOYSTICK equ 0x00000200 ; joystick subsystem
                SDL_INIT_HAPTIC equ 0x00001000 ; haptic (force feedback) subsystem
                SDL_INIT_GAMECONTROLLER equ 0x00002000 ; controller subsystem
                SDL_INIT_EVENTS  equ 0x00004000 ; events subsystem
                SDL_INIT_NOPARACHUTE equ 0x00100000 ; don't catch fatal signals

                SDL_INIT_EVERYTHING equ SDL_INIT_TIMER \
                                                        + SDL_INIT_AUDIO \
                                                        + SDL_INIT_VIDEO \
                                                        + SDL_INIT_JOYSTICK \
                                                        + SDL_INIT_HAPTIC \
                                                        + SDL_INIT_GAMECONTROLLER \
                                                        + SDL_INIT_EVENTS

        ; SDL Window  ------------------------------------------------

                SDL_WINDOW_SHOWN equ 0x00000004
                SDL_WINDOWPOS_CENTERED_MASK equ 0x2FFF0000

        ; SDL Renerer ------------------------------------------------

                SDL_RENDERER_ACCELERATED equ 0x00000002
                SDL_RENDERER_PRESENTVSYNC equ 0x00000004

        ; SDL Event type ------------------------------------------------

                SDL_QUIT equ 0x100
                SDL_KEYDOWN equ 0x300

; # Import functions by using our predefined macro

        importfunc user32.dll, MessageBoxA
        importfunc kernel32.dll, ExitProcess

        importfunc sdl2.dll, SDL_Init
        importfunc sdl2.dll, SDL_ClearError
        importfunc sdl2.dll, SDL_GetError
        importfunc sdl2.dll, SDL_CreateWindow
        importfunc sdl2.dll, SDL_CreateRenderer
        importfunc sdl2.dll, SDL_RWFromFile
        importfunc sdl2.dll, SDL_LoadBMP_RW
        importfunc sdl2.dll, SDL_CreateTextureFromSurface
        importfunc sdl2.dll, SDL_FreeSurface
        importfunc sdl2.dll, SDL_RenderClear
        importfunc sdl2.dll, SDL_RenderCopy
        importfunc sdl2.dll, SDL_RenderPresent
        importfunc sdl2.dll, SDL_Delay

        importfunc sdl2.dll, SDL_PollEvent
        importfunc sdl2.dll, SDL_Quit
 

;# Data segment
; ---------------------------------------------------------------
segment .data use32
       
        txt_bmp_path db "hello.bmp",0
        txt_file_access_rb db "rb",0,0
        txt_error db "! Error: ",0
        txt_window db "Hello World: J.K. Encryptor 256, October 02, year of 2013.",0

       
;# Uninitialized data segment
; ---------------------------------------------------------------
segment .bss use32
       
        hWindow resd 1
        hRenderer resd 1
        hFile resd 1
        hBitmap resd 1
        hTexture resd 1

        hEventBuffer:
                        .type resd 1
                        .other resb 256

;# Code segment
; ---------------------------------------------------------------
segment .text use32
       
;Info: This "start" label will tell, the compiler, where execution begins.


..start:
       
        ; # SDL_Init:
        ; # Use this function to initialize the SDL library.
        ; # This must be called before using any other SDL function.
        ; # --------------------------------------------------------------------------

                push dword SDL_INIT_EVERYTHING

                call [SDL_Init]

                add esp,dword 4*1 ; pop pushed items off the stack

        ; # SDL_ClearError:
        ; # Use this function to clear any previous error message.
        ; # --------------------------------------------------------------------------

                call [SDL_ClearError]

        ; # SDL_CreateWindow:
        ; # Use this function to create a window
        ; # with the specified position, dimensions, and flags.
        ; # --------------------------------------------------------------------------

                push dword SDL_WINDOW_SHOWN
                push dword 480
                push dword 640
                push dword SDL_WINDOWPOS_CENTERED_MASK
                push dword SDL_WINDOWPOS_CENTERED_MASK
                push dword txt_window

                call [SDL_CreateWindow]

                add esp,dword 4*6 ; pop pushed items off the stack

                mov dword [hWindow],eax

        ; # SDL_CreateRenderer:
        ; # Use this function to create a 2D rendering context for a window.
        ; # --------------------------------------------------------------------------

                push dword (SDL_RENDERER_ACCELERATED+SDL_RENDERER_PRESENTVSYNC)
                push dword -1
                push dword [hWindow]

                call [SDL_CreateRenderer]

                add esp,dword 4*3 ; pop pushed items off the stack

                mov dword [hRenderer],eax

        ; # SDL_LoadBMP:
        ; # Use this function to load a surface from a BMP file.
        ; # --------------------------------------------------------------------------

                ; # SDL_RWFromFile:
                ; # Use this function to create a new SDL_RWops structure for
                ; # reading from and/or writing to a named file.
                ; -----------------------------------------------------------------

                        push dword txt_file_access_rb
                        push dword txt_bmp_path

                        call [SDL_RWFromFile]

                        add esp,dword 4*2 ; pop pushed items off the stack

                        mov dword [hFile],eax

                ; # SDL_LoadBMP_RW:
                ; # Use this function to load a BMP image
                ; # from a seekable SDL data stream (memory or file).
                ; -----------------------------------------------------------------

                        push dword 1
                        push dword [hFile]

                        call [SDL_LoadBMP_RW]

                        add esp,dword 4*2 ; pop pushed items off the stack

                        mov dword [hBitmap],eax

        ; # SDL_CreateTextureFromSurface:
        ; # Use this function to create a texture from an existing surface.
        ; # --------------------------------------------------------------------------

                push dword [hBitmap]
                push dword [hRenderer]

                call [SDL_CreateTextureFromSurface]

                add esp,dword 4*2 ; pop pushed items off the stack

                mov dword [hTexture],eax

        ; # SDL_FreeSurface:
        ; # Use this function to free an RGB surface.
        ; # --------------------------------------------------------------------------

                push dword [hBitmap]

                call [SDL_FreeSurface]

                add esp,dword 4*1 ; pop pushed items off the stack

        ; # SDL_RenderClear:
        ; # Use this function to clear the current rendering target with the drawing color.
        ; # --------------------------------------------------------------------------

                push dword [hRenderer]

                call [SDL_RenderClear]

                add esp,dword 4*1 ; pop pushed items off the stack

        ; # SDL_RenderCopy:
        ; # Use this function to copy a portion
        ; # of the texture to the current rendering target.
        ; # --------------------------------------------------------------------------

                push dword 0
                push dword 0
                push dword [hTexture]
                push dword [hRenderer]

                call [SDL_RenderCopy]

                add esp,dword 4*4 ; pop pushed items off the stack

        ; # SDL_RenderPresent:
        ; # Use this function to update the screen with rendering performed.
        ; # --------------------------------------------------------------------------

                push dword [hRenderer]

                call [SDL_RenderPresent]

                add esp,dword 4*1 ; pop pushed items off the stack


        ; # Main loop:
        ; # --------------------------------------------------------------------------

        .mainLoop:

                ; # SDL_PollEvent:
                ; # Use this function to poll for currently pending events.
                ; # --------------------------------------------------------------------------
                       
                        push dword hEventBuffer

                        call [SDL_PollEvent]

                        add esp,dword 4*1 ; pop pushed items off the stack

                        cmp eax,dword 0

                        jle .noEvents

                ; # SDL_QUIT:
                ; # --------------------------------------------------------------------------

                        cmp dword [hEventBuffer.type], dword SDL_QUIT

                        je .quitMainLoop

                ; # SDL_KEYDOWN:
                ; # --------------------------------------------------------------------------

                        cmp dword [hEventBuffer.type], dword SDL_KEYDOWN

                        je .quitMainLoop

                ; # --------------------------------------------------------------------------

                .noEvents:
               

                jmp .mainLoop

        .quitMainLoop:

        ; # SDL_Quit:
        ; # Use this function to clean up all initialized subsystems.
        ; # You should call it upon all exit conditions.
        ; # --------------------------------------------------------------------------

                call [SDL_Quit]

        ; # ExitProcess:
        ; # --------------------------------------------------------------------------

                push dword 0 ; Set Return value

                call [ExitProcess]

; ----
; END
; ----


.VIDEO:

Tired of reading?

Watch video on youtube named: "SDL 2 Hello World NASM ALINK Win32 API"

Link: http://youtu.be/aHc6x7LQKvg

.CODE:

Added attachment of code file, bmp file, dll file.

.END:

Bye!

Have a nice day and let bytes protect you! :D

Encryptor256
Encryptor256's Investigation \ Research Department.