Here is my code again so it stays within thread context

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
)


> On Dec 1, 2020, at 5:33 AM, emacstheviking <obji...@gmail.com> wrote:
> 
> For me, I have settled for being able to stop the event loop, edit the code
> then resume the event loop where I left off and that's working well for me.
> 
> basically I have u (for UI!) -routines
>  uprep''
>  ugo 0 means when SDL_QUIT is received, leave everything up, ugo 1 will
> call sdl_destroywindow and sdl_quit
>  uend''
> 
> and some basic run./stop code:
> 
> uend=: 3 : 0
> sdl_freesurface sfc
> sdl_destroyrenderer^:(appr>0) appr
> sdl_destroywindow^:(appw>0) appw
> sdl_quit''
> smoutput 'sdl shut down'
> 'appw appr'=: 0 0
> )
> 
> ugo =: 3 : 0
> sdlprep SCREEN_X_MAX;SCREEN_Y_MAX
> urun''
> uend^:y ''
> )
> 
> On Tue, 1 Dec 2020 at 07:25, Thomas McGuire <tmcguir...@gmail.com> wrote:
> 
>> I was glad to see some renewed interest in SDL2 and thank you to Raul
>> Miller for the suggestion of setting timer to run an event loop. I haven’t
>> had the chance to try Bill Lam’s suggestion of creating a tight event loop
>> under Jconsole yet.
>> 
>> I have attached my code for a simple example that:
>> opens a window,
>> displays the toucan.bmp that comes with J,
>> and then allows you to quit by pressing the close window control on the
>> displayed window.
>> 
>> The only gotcha at this point is that a subsequent rerun under Jqt, the
>> event system in SDL2 doesn’t register any events. So the app will hang and
>> the window stays open. I have to quit Jqt entirely and restart to rerun the
>> application properly. This is minimally annoying from a debugging stand
>> point. Jqt comes back up quickly enough after a shut down and coding using
>> a dll library can cause its share of hanging the entire Jqt interface when
>> mistakes in the dll wrapper are made.
>> 
>> I presume I am running into threading/Jqt state issues or perhaps the call
>> to SDL_Quit is not running like I think it is. At any rate for my purposes
>> I am pretty happy with the result. It has been enhancing my understanding
>> of Graphics/Game programming issues while I continue to work in J.
>> 
>> 
>> ----------------------------------------------------------------------
>> For information about J forums see http://www.jsoftware.com/forums.htm
>> 
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to