NB. sdlbmpevtex.ijs - example of setting up a bitmap displayed in window 
NB.                   under SDL with QUIT event processing

load '~/j902-user/temp/sdl2.ijs'

NB. SDL_Init <SDL_INIT_VIDEO




sdlbmpevtex =: 3 : 0
   NB. to handle events SDL needs 56 bytes of space to fit the 
   NB. event into. This is passed as a pointer. Defined globally
   NB. needs to be freed when you quit the application
   NB. Event loop variables
   ev =: mema 56

   NB. initialized pointer variables to be used with SDL2 dll
   window =: null     
   bmpsurf =: null
   texture =: null
   renderer =: null

   NB. calls to setup SDL2 and create a window with bitmap
   SDL_Init <SDL_INIT_EVERYTHING

   window =:  {. SDL_CreateWindow 'SDL2 from J'; 
SDL_WINDOWPOS_UNDEFINED;SDL_WINDOWPOS_UNDEFINED;660;480;0
   if. window = null do.
     smoutput 'ERROR: failed to create window'
     return.
   end.
   renderer =: {. SDL_CreateRenderer window; _1; 0
   if. renderer = null do.
     smoutput 'ERROR: failed to create renderer'
     return.
   end.

   SDL_SetRenderDrawColor renderer; 0; 0; 0; 255
   SDL_RenderClear<renderer
  
   NB. The SDL2 API call SDL_LoadBMP is a macro of 2 lower level
   NB. calls. It is created as a Jfunction in this 
   bmpsurf=: {. SDL_LoadBMP <'/Applications/j902/addons/graphics/bmp/toucan.bmp'
   if. bmpsurf = null do.
     smoutput 'ERROR: failed to load bitmap'
     return.
   end.

   texture =: {. SDL_CreateTextureFromSurface renderer; <bmpsurf
   if. texture = null do.
     smoutput 'ERROR: failed to create texture'
     return.
   end.

   SDL_FreeSurface <bmpsurf
   bmpsurf =: null

   NB. Copy the texture to the renderer and present to the window
   SDL_RenderCopy renderer;texture;null;<null
   SDL_RenderPresent<renderer

   NB. set timer handler and start timer
   sys_timer_z_ =: sdleventhdlr_base_
   wd 'timer 1000'
)

sdleventhdlr =: 3 : 0
     NB. Look for a quit event and call sdlfullquit and exit
     NB. Stop the timer while we process (it avoid forever errors)
     wd 'timer 0'
     while. 0 ~: SDL_PollEvent <<ev do.
       evtype =: {. 0 (3!:4) memr ev,0,32
NB.       smoutput 'evtype = ',":evtype
       if. evtype = SDL_QUIT do.
         NB. quit and return timer stays off
         sdlfullquit''
         return.
       end.
     end.

     NB. restart the timer
     wd 'timer 1000'
)

sdlfullquit =: 3 : 0
SDL_DestroyTexture<texture
texture =: null
SDL_DestroyRenderer<renderer
renderer =: null
SDL_DestroyWindow <window
window =: null
SDL_Quit ''
memf ev
ev =: 0
)
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to