This is section 3, the content of the main compile file, code was organized into three sections.
; ############################################################
; ############################################################
; 3. Section: MAIN CORE IN NASM
; ############################################################
; ############################################################
; Tell compiler what kind'a cpu type
cpu 386
; Tell compiler to generate 32bit code
bits 32
; # Define GLFW functions and constants:
; ----------------------------------------------------
cexternList glfwInit, \
glfwTerminate, \
glfwSetErrorCallback, \
glfwCreateWindow, \
glfwMakeContextCurrent, \
glfwWindowShouldClose, \
glfwSwapBuffers, \
glfwPollEvents, \
glfwSetWindowSizeCallback, \
glfwGetFramebufferSize
; # Define C functions and constants
; ----------------------------------------------------
cexternList atexit, \
printf, \
fopen, \
fread, \
fclose, \
malloc, \
free, \
fseek
cglobal main
SEEK_CUR equ 1
; # Define OpenGL functions and constants
; ----------------------------------------------------
; GL externs (These functions are imported from library)
define_gl_func glClear,4
define_gl_func glClearColor,16
define_gl_func glViewport,16
define_gl_func glMatrixMode,4
define_gl_func glLoadIdentity,0
define_gl_func glOrtho,48
define_gl_func glBegin,4
define_gl_func glColor3f,12
define_gl_func glVertex2f,8
define_gl_func glEnd,0
define_gl_func glEnable,4
define_gl_func glDisable,4
define_gl_func glTexCoord2f,8
define_gl_func glBindTexture,8
define_gl_func glTexImage2D,36
define_gl_func glGenTextures,8
define_gl_func glTexParameteri,12
define_gl_func glDeleteTextures,8
define_gl_func glGetError,0
; GL error constants
GL_NO_ERROR equ 0
GL_INVALID_ENUM equ 0x0500
GL_INVALID_VALUE equ 0x0501
GL_INVALID_OPERATION equ 0x0502
GL_STACK_OVERFLOW equ 0x0503
GL_STACK_UNDERFLOW equ 0x0504
GL_OUT_OF_MEMORY equ 0x0505
; GL graphical constants
GL_COLOR_BUFFER_BIT equ 0x00004000
GL_TRUE equ 1
GL_FALSE equ 0
GL_MODELVIEW equ 0x1700
GL_PROJECTION equ 0x1701
GL_TRIANGLES equ 0x0004
GL_TEXTURE_2D equ 0x0DE1
GL_QUADS equ 0x0007
GL_RGB equ 0x1907
GL_UNSIGNED_BYTE equ 0x1401
GL_TEXTURE_MAG_FILTER equ 0x2800
GL_TEXTURE_MIN_FILTER equ 0x2801
GL_LINEAR equ 0x2601
GL_BGR_EXT equ 0x80E0
; -------------------------------------------------------------------------------------
section .data use32 ; 32bit Data segment
; -------------------------------------------------------------------------------------
; Strings:
; -------------------------------------------------------------------------------
txt_bmp_info: db 10,"Bitmap '%s' info (Width: %d, Height: %d,"
db "Compression: %d, BitCount: %d, DataSize: %d).",0
txt_window_text: db "Hello world with GLFW, NASM and Encryptor256",0
txt_esp: db 10,"Stack check: %d is equal? %d",0
txt_bmpname: db "helloworld.bmp",0
txt_error: db 10,"~ Error: %s",0
txt_file_access_rb: db "rb",0
; Strings: glGetError:
; -------------------------------------------------------------------------------
txt_glGetError: db 10,"glGetError: 0x%X, %s.",0
txt_glErrorN: db "GL_NO_ERROR",0
txt_glError0: db "GL_INVALID_ENUM",0
txt_glError1: db "GL_INVALID_VALUE",0
txt_glError2: db "GL_INVALID_OPERATION",0
txt_glError3: db "GL_STACK_OVERFLOW",0
txt_glError4: db "GL_STACK_UNDERFLOW",0
txt_glError5: db "GL_OUT_OF_MEMORY",0
; Determine OpenGL error type by this jump table
glError_types: dd txt_glError0,txt_glError1,txt_glError2
dd txt_glError3,txt_glError4,txt_glError5
; Memory items:
; -------------------------------------------------------------------------------
hWindow: dd 0
bitmapW: dd 0
bitmapH: dd 0
hBitmapData: dd 0
textureID: dd 0
; One for the debug purposes
espBK: dd 0
; -------------------------------------------------------------------------------------
section .text use32 ; 32bit Code segment
; -------------------------------------------------------------------------------------
main:
push ebp
mov ebp,esp
; Save stack pointer, if error occurs, then compare, maybe stack error.
mov dword [espBK],esp
; glfwSetErrorCallback: (GLFW)
; -------------------------------------------------------------------------------
; This function sets the error callback.
; -------------------------------------------------------------------------------
invoke_cdecl glfwSetErrorCallback,GLFWErrorCallbackFunction
; glfwInit: (GLFW)
; -------------------------------------------------------------------------------
; This function initializes the GLFW library.
; -------------------------------------------------------------------------------
invoke_cdecl glfwInit
invoke_error je,GL_FALSE,main.quitError
; glfwTerminate: (GLFW)
; -------------------------------------------------------------------------------
; This function destroys all remaining windows,
; frees any allocated resources and sets the library to an uninitialized state.
; -------------------------------------------------------------------------------
invoke_cdecl atexit,glfwTerminate
; glfwCreateWindow: (GLFW)
; -------------------------------------------------------------------------------
; This function creates a window and its associated context.
; -------------------------------------------------------------------------------
invoke_cdecl glfwCreateWindow,1024,512,txt_window_text,0,0
invoke_error je,0,main.quitError
invoke_returns_at [hWindow]
; glfwSetWindowSizeCallback: (GLFW)
; -------------------------------------------------------------------------------
; This function sets the size callback of the specified window,
; which is called when the window is resized.
; -------------------------------------------------------------------------------
invoke_cdecl glfwSetWindowSizeCallback,[hWindow],GLFWWindowSizeCallback
; glfwMakeContextCurrent: (GLFW)
; -------------------------------------------------------------------------------
; TThis function makes the context of the specified window current on the calling thread
; -------------------------------------------------------------------------------
invoke_cdecl glfwMakeContextCurrent,[hWindow]
; * This early GL setup...
; glViewport: (OpenGL)
; -------------------------------------------------------------------------------
; The glViewport function sets the viewport.
; -------------------------------------------------------------------------------
invoke_stdcall glViewport,0,0,1024,512
; glMatrixMode: (OpenGL)
; -------------------------------------------------------------------------------
; The glMatrixMode function specifies which matrix is the current matrix.
; -------------------------------------------------------------------------------
invoke_stdcall glMatrixMode,GL_PROJECTION
; glLoadIdentity: (OpenGL)
; -------------------------------------------------------------------------------
; The glLoadIdentity function replaces the current matrix with the identity matrix.
; -------------------------------------------------------------------------------
invoke_stdcall glLoadIdentity
; glOrtho: (OpenGL)
; -------------------------------------------------------------------------------
; The glOrtho function multiplies the current matrix by an orthographic matrix.
; -------------------------------------------------------------------------------
float32.args64i 0,1024,512,0,0,1
invoke_stdcall glOrtho
; glMatrixMode: (OpenGL)
; -------------------------------------------------------------------------------
; The glMatrixMode function specifies which matrix is the current matrix.
; -------------------------------------------------------------------------------
invoke_stdcall glMatrixMode,GL_MODELVIEW
; glLoadIdentity: (OpenGL)
; -------------------------------------------------------------------------------
; The glLoadIdentity function replaces the current matrix with the identity matrix.
; -------------------------------------------------------------------------------
invoke_stdcall glLoadIdentity
; * This is where image loading and image setup begins...
; LoadBmpFile: (Custom)
; -------------------------------------------------------------------------------
; Load bitmap image and obtain data.
; -------------------------------------------------------------------------------
invoke_stdcall LoadBmpFile, \
txt_bmpname, \
txt_file_access_rb, \
bitmapW,bitmapH, hBitmapData
invoke_error jne,0,main.quitError
; glGenTextures: (OpenGL)
; -------------------------------------------------------------------------------
; The glGenTextures function generates texture names.
; -------------------------------------------------------------------------------
invoke_stdcall glGenTextures,1,textureID
invoke_error je,0,main.quitError
; glBindTexture: (OpenGL)
; -------------------------------------------------------------------------------
; The glBindTexture function enables the creation
; of a named texture that is bound to a texture target.
; -------------------------------------------------------------------------------
invoke_stdcall glBindTexture,GL_TEXTURE_2D,[textureID]
; glTexImage2D: (OpenGL)
; -------------------------------------------------------------------------------
; The glTexImage2D function specifies a two-dimensional texture image.
; -------------------------------------------------------------------------------
invoke_stdcall glTexImage2D,GL_TEXTURE_2D,0, \
GL_RGB,[bitmapW],[bitmapH],0, \
GL_BGR_EXT,GL_UNSIGNED_BYTE,[hBitmapData]
; glTexParameteri: (OpenGL)
; -------------------------------------------------------------------------------
; Sets texture parameters.
; -------------------------------------------------------------------------------
invoke_stdcall glTexParameteri,GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR
invoke_stdcall glTexParameteri,GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR
; glBindTexture: (OpenGL)
; -------------------------------------------------------------------------------
invoke_stdcall glBindTexture,GL_TEXTURE_2D,0
; Enter main loop:
; -------------------------------------------------------------------------------
main.messageLoop:
; glClearColor: (OpenGL)
; -------------------------------------------------------------------------------
; The glClearColor function specifies clear values for the color buffers.
; -------------------------------------------------------------------------------
float32.args32 0.84765625,0.875,0.9375,0.0
invoke_stdcall glClearColor
; glClear: (OpenGL)
; -------------------------------------------------------------------------------
; The glClear function clears buffers to preset values.
; -------------------------------------------------------------------------------
invoke_stdcall glClear,GL_COLOR_BUFFER_BIT
; Display image: BEGIN
; -------------------------------------------------------------------------------
float32.args32 1.0,1.0,1.0
invoke_stdcall glColor3f
invoke_stdcall glEnable,GL_TEXTURE_2D
invoke_stdcall glBindTexture,GL_TEXTURE_2D,[textureID]
invoke_stdcall glBegin,GL_QUADS
; Get window size
sub esp,dword 4
mov edx,esp
sub esp,dword 4
mov eax,esp
invoke_cdecl glfwGetFramebufferSize,[hWindow],eax,edx
pop dword eax ; W
pop dword edx ; H
; Push arguments
float32.args32i 0,edx ; 8. Arguments for glVertex2f
float32.args32i 0,1 ; 7. Arguments for glTexCoord2f
float32.args32i eax,edx ; 6. Arguments for glVertex2f
float32.args32i 1,1 ; 5. Arguments for glTexCoord2f
float32.args32i eax,0 ; 4. Arguments for glVertex2f
float32.args32i 1,0 ; 3. Arguments for glTexCoord2f
float32.args32i 0,0 ; 2. Arguments for glVertex2f
float32.args32i 0,0 ; 1. Arguments for glTexCoord2f
; Call functions, set texture and vertex coordinates
invoke_stdcall glTexCoord2f ; 1.
invoke_stdcall glVertex2f ; 2.
invoke_stdcall glTexCoord2f ; 3.
invoke_stdcall glVertex2f ; 4.
invoke_stdcall glTexCoord2f ; 5.
invoke_stdcall glVertex2f ; 6.
invoke_stdcall glTexCoord2f ; 7.
invoke_stdcall glVertex2f ; 8.
invoke_stdcall glEnd
invoke_stdcall glDisable,GL_TEXTURE_2D
; Display image: END
; -------------------------------------------------------------------------------
; glfwWindowShouldClose: (GLFW)
; -------------------------------------------------------------------------
; This function returns the value of the close flag of the specified window.
; -------------------------------------------------------------------------
invoke_cdecl glfwWindowShouldClose,[hWindow]
invoke_error jg,0,main.quitLoop
; glfwSwapBuffers: (GLFW)
; -------------------------------------------------------------------------
; This function swaps the front and back buffers of the specified window.
; -------------------------------------------------------------------------
invoke_cdecl glfwSwapBuffers,[hWindow]
; glfwPollEvents: (GLFW)
; -------------------------------------------------------------------------
; This function processes only those events that
; have already been received and then returns immediately.
; -------------------------------------------------------------------------
invoke_cdecl glfwPollEvents
jmp main.messageLoop
main.quitLoop:
; free: (C)
; -------------------------------------------------------------------------
; C function, release bitmap data, that were allocated by Load bitmap function
; -------------------------------------------------------------------------
invoke_cdecl free,[hBitmapData]
; glDeleteTextures: (OpenGL)
; -------------------------------------------------------------------------
; The glDeleteTextures function deletes named textures.
; -------------------------------------------------------------------------
invoke_stdcall glDeleteTextures,1,textureID
; Quit normal:
; -------------------------------------------------------------------------------
mov eax,dword 0
pop ebp
ret
; Quit error:
; -------------------------------------------------------------------------------
main.quitError:
; Check, print stack error:
mov eax,esp
mov edx,esp
mov edx,dword [espBK]
invoke_cdecl printf,txt_esp,edx,eax
; Check stack error:
call printGLError
mov eax,dword -1
pop ebp
ret