' ' This is an image loader plugin for the CANVAS context. It uses the STB libraries. PvE - MIT License 2015. ' ' Updated to use textures instead of the deprecated glDrawPixels - October 2016 - PvE - MIT License 2016. ' ' Web address: https://github.com/nothings/stb ' ' STB_IMAGE_STRIP(file$, x, y, total, nr) ' file$: name of file to load ' xpos: x position of the centre of the picture on the canvas ' ypos: y position of the centre of the pictureon the canvas ' total: the total amount of frames in this picture ' nr: which actual frame to show, 0=first frame, 1=second frame etc ' ' STB_IMAGE(file$, xpos, ypos) ' file$: name of file to load ' xpos: x position of the centre of the picture on the canvas ' ypos: y position of the centre of the picture on the canvas ' ' STB_IMAGE_INFO(file$) ---> print information on console about picture ' file$: name of file ' ' STB_IMAGE_WIDTH(file$) ' file$: name of file ' ' STB_IMAGE_HEIGHT(file$) ' file$: name of file ' ' STB_IMAGE_FREE(file$) ' file$: name of file ' '----------------------------------------------------------------------------------------------------------------------------------- ' ' The next line needs a correct location to the Public Domain 'stb_image.h' file. PRAGMA INCLUDE stb-master/stb_image.h : ' Include the stb_image header file '----------------------------------------------------------------------------------------------------------------------------------- ' Do not change anything from here. PRAGMA OPTIONS -DSTB_IMAGE_IMPLEMENTATION -DSTBI_FAILURE_USERMSG : ' Enable graphical functions and user friendly error messages PROTO stbi_failure_reason : ' Make the call 'stbi_failure_reason' known to BaCon PROTO stbi_image_free : ' Make the call 'stbi_image_free' known to BaCon PROTO stbi_info : ' Make the call 'stbi_info' known to BaCon CONST STB_CHANNELS = 4 : ' Allocate 4 color channels DECLARE STB_IMAGE_LOADED ASSOC int : ' To remember if this file was already loaded DECLARE STB_IMAGE_STORE ASSOC unsigned char* : ' Memory pointer to parsed file '----------------------------------------------------------------------------------------------------------------------------------- SUB STB_IMAGE_STRIP(file$, xpos, ypos, total, nr) LOCAL width, height, framew, channels, texture TYPE int LOCAL data TYPE unsigned char* IF STB_IMAGE_LOADED(file$) = FALSE THEN data = stbi_load(file$, &width, &height, &channels, STB_CHANNELS) : ' Load the actual image STB_IMAGE_STORE(file$) = data STB_IMAGE_LOADED(file$) = TRUE ELSE data = STB_IMAGE_STORE(file$) : ' Data already known stbi_info(file$, &width, &height, &channels) ENDIF IF data = NULL THEN PRINT stbi_failure_reason() FORMAT "ERROR: %s.\n" : ' If loading image fails, print the reason END 1 : ' ...and exit the program. ENDIF IF total <= 0 THEN EXIT SUB : ' No frame to display, exit silently framew = width/total : ' Calculate width of one frame CALL Draw_Prepare : ' Setup 2D canvas with (0,0) top left glPixelStorei(GL_UNPACK_ROW_LENGTH, width) : ' Set the row length of the data array glGenTextures(1, &texture) glBindTexture(GL_TEXTURE_2D, texture) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) glTexImage2D(GL_TEXTURE_2D, 0, channels, framew, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data+framew*STB_CHANNELS*nr) glColor4ub(255,255,255,255) : ' Prevent color mixup with the texture glEnable(GL_TEXTURE_2D) glBegin(GL_QUADS) glTexCoord2f(0.0f, 0.0f) : glVertex2f(xpos-framew/2, ypos-height/2) glTexCoord2f(0.0f, 1.0f) : glVertex2f(xpos-framew/2, ypos+height/2-1) glTexCoord2f(1.0f, 1.0f) : glVertex2f(xpos+framew/2, ypos+height/2-1) glTexCoord2f(1.0f, 0.0f) : glVertex2f(xpos+framew/2, ypos-height/2) glEnd() glDisable(GL_TEXTURE_2D) glBindTexture(GL_TEXTURE_2D, 0) glDeleteTextures(1, &texture) glPixelStorei(GL_UNPACK_ROW_LENGTH, 0) : ' Restore the row length ENDSUB SUB STB_IMAGE(file$, xpos, ypos) STB_IMAGE_STRIP(file$, xpos, ypos, 1, 0) : ' Load one picture ENDSUB FUNCTION STB_IMAGE_WIDTH(file$) LOCAL width, height, framew, n TYPE int stbi_info(file$, &width, &height, &n) RETURN width ENDFUNCTION FUNCTION STB_IMAGE_HEIGHT(file$) LOCAL width, height, framew, n TYPE int stbi_info(file$, &width, &height, &n) RETURN height ENDFUNCTION SUB STB_IMAGE_INFO(file$) LOCAL width, height, framew, n TYPE int stbi_info(file$, &width, &height, &n) PRINT "Filename: ", file$ PRINT "Dimensions: ", width, "x", height PRINT "Channels: ", n ENDSUB SUB STB_IMAGE_FREE(file$) STB_IMAGE_LOADED(file$) = FALSE stbi_image_free(STB_IMAGE_STORE(file$)) : ' Release memory ENDSUB