I happened to be playing around with SDL2 and J and came across your thread. 
While you probably got through your issues and don’t care any more, still, I 
thought I would complete the thread with an answer so future queries into the 
archive can provide some more detail to interested parties.

First the SDL2 tutorials at Gigi Labs seem to be easier to translate into J 
code than the Lazy foo tutorials. Find the tutorial to load a bitmap here:

http://gigi.nullneuron.net/gigilabs/displaying-an-image-in-an-sdl2-window/ 
<http://gigi.nullneuron.net/gigilabs/displaying-an-image-in-an-sdl2-window/>

Now the following code works on a MacBook Pro with Catalina loaded. I 
downloaded the SDL2 and built it and was able to bootstrap up calls into DLL 
from J. I have some issues with the way it works as I have to use take ({.) to 
grab the pointer I need from the result of a call to SDL2. There are also J 
boxing idiosyncrasies that mess with your pointers. 

So here’s the code to get the toucan bitmap that is in the addons/graphics/bmp 
directory into an SDL2 window:

load ‘path to SDL2.ijs’

NB. The following are defined in SDL2.ijs
NB. void=: 0$''
NB. null=: <0

SDL_Init <SDL_INIT_VIDEO
window =: {. SDL_CreateWindow 'SDL2 from J’; 
SDL_WINDOWPOS_UNDEFINED;SDL_WINDOWPOS_UNDEFINED;660;480;0
renderer =: {. SDL_CreateRenderer window; _1; 0

NB. In SDL2 SDL_LoadBMP is a C-language macro that uses the following 2 calls 
Rwops =: {. SDL_RWFromFile y;'rb'
gHelloToucan =: {. SDL_LoadBMP_RW Rwops;1

texture =: {. SDL_CreateTextureFromSurface renderer;<gHelloToucan

NB. Here is a boxing idiosyncrasy
NB. null =: <0
NB. The list needs to be boxed elements within a boxed list
NB. ; just concatenates the last boxed element on a boxed list. So it 
NB. Needs to be double boxed
SDL_RenderCopy renderer;texture;null;<null

NB. Now display it
SDL_RenderPresent <renderer

NB. To kill the window
SDL_DestroyTexture <texture

SDL_DestroyRenderer <renderer
SDL_DestroyWindow <window
SDLQuit void

——————————————————————————————————————
SDL2.ijs
NB. This is the DLL access definitions
require 'dll'

void=: 0$''
null=: <0

SDLDLL =: '/usr/local/lib/libSDL2.dylib'

NB. SDL Constants
SDL_INIT_TIMER=: 16b00000001
SDL_INIT_AUDIO=: 16b00000010
SDL_INIT_VIDEO=: 16b00000020 NB. SDL_INIT_VIDEO implies SDL_INIT_EVENTS */
SDL_INIT_JOYSTICK=: 16b00000200 NB. SDL_INIT_JOYSTICK implies SDL_INIT_EVENTS */
SDL_INIT_HAPTIC=: 16b00001000
SDL_INIT_GAMECONTROLLER=: 16b00002000 NB. SDL_INIT_GAMECONTROLLER implies 
SDL_INIT_JOYSTICK */
SDL_INIT_EVENTS=: 16b00004000
SDL_INIT_SENSOR=: 16b00008000
SDL_INIT_NOPARACHUTE=: 16b00100000 NB. compatibility; this flag is ignored. */
SDL_INIT_EVERYTHING=: SDL_INIT_TIMER +. SDL_INIT_AUDIO +. SDL_INIT_VIDEO | 
SDL_INIT_EVENTS +. SDL_INIT_JOYSTICK +. SDL_INIT_HAPTIC +. 
SDL_INIT_GAMECONTROLLER +. SDL_INIT_SENSOR

NB. extern DECLSPEC int SDLCALL SDL_Init(Uint32 flags);
SDL_Init =: (SDLDLL, ' SDL_Init > i i')&cd

NB. extern DECLSPEC int SDLCALL SDL_InitSubSystem(Uint32 flags);
SDL_InitSubSystem =: (SDLDLL, ' SDL_InitSubSystem i i')&cd

NB. extern DECLSPEC void SDLCALL SDL_QuitSubSystem(Uint32 flags);
SDL_QuitSubSystem =: (SDLDLL, ' SDL_QuitSubSystem n i')&cd

NB. extern DECLSPEC Uint32 SDLCALL SDL_WasInit(Uint32 flags);
SDL_WasInit =: (SDLDLL, ' SDL_WasInit i i')&cd

NB. extern DECLSPEC void SDLCALL SDL_Quit(void);
SDLQuit =: (SDLDLL, ' SDL_Quit n')&cd


SDL_WINDOWPOS_UNDEFINED_MASK =: 16b1fff0000
SDL_WINDOWPOS_UNDEFINED =: SDL_WINDOWPOS_UNDEFINED_MASK +. 0

NB. extern DECLSPEC SDL_Window * SDLCALL SDL_CreateWindow(const char *title,
NB. int x, int y, int w,
NB. int h, Uint32 flags);
SDL_CreateWindow =: (SDLDLL, ' SDL_CreateWindow * &c i i i i i')&cd

NB. extern DECLSPEC SDL_Renderer * SDLCALL SDL_CreateRenderer(SDL_Window * 
window, int index, Uint32 flags);
SDL_CreateRenderer =: (SDLDLL, ' SDL_CreateRenderer * * i i')&cd

SDL_SetRenderDrawColor =: (SDLDLL, ' SDL_SetRenderDrawColor n * i i i i')&cd

SDL_RenderCopy =: (SDLDLL, ' SDL_RenderCopy n * * * *')&cd

SDL_RenderClear =: (SDLDLL, ' SDL_RenderClear n *')&cd

SDL_RenderPresent =: (SDLDLL, ' SDL_RenderPresent n *')&cd

SDL_RenderDrawLine =: (SDLDLL, ' SDL_RenderDrawLine n * i i i i')&cd
SDL_DestroyRenderer =: (SDLDLL, ' SDL_DestroyRenderer n *')&cd
SDL_DestroyWindow =: (SDLDLL, ' SDL_DestroyWindow n *')&cd
SDL_DestroyTexture =: (SDLDLL, ' SDL_DestroyTexture n *')&cd
SDL_FreeSurface =: (SDLDLL, ' SDL_FreeSurface n *')&cd

SDL_CreateTextureFromSurface =: (SDLDLL, ' SDL_CreateTextureFromSurface * * 
*')&cd

SDL_GetWindowSurface =: (SDLDLL, ' SDL_GetWindowSurface * *')&cd
SDL_RWFromFile =: (SDLDLL, ' SDL_RWFromFile * *c *c')&cd
SDL_LoadBMP_RW =: (SDLDLL, ' SDL_LoadBMP_RW * * i')&cd
SDL_BlitSurface =: (SDLDLL, ' SDL_UpperBlit > i * * * *')%cd




> On Aug 8, 2015, at 9:51 AM, Jon Hough <jgho...@outlook.com> wrote:
> 
> Thanks,
> Yes, I will look into cder. 
> Regards,Jon
> 
>> Date: Sat, 8 Aug 2015 21:40:16 +0800
>> From: bbill....@gmail.com
>> To: programm...@jsoftware.com
>> Subject: Re: [Jprogramming] J calling C++ SDL
>> 
>> I think your code will not work on 64-bit platforms. You need to figure out
>> which parameters will be 64-bit.
>> 
>> when using c, the c compiler will figure this out for you, but when using
>> J, you are on you own.
>> 
>> if cd raise domain error, cder'' will tell why is failed.
>> 
>> please study the j user manual.
>> 
>> I am really just experimenting here. SDL is a well known C/C++ media
>> library. https://www.libsdl.org/download-2.0.php
>> It is possible to use SDL for all kinds of things, but it is often used for
>> games and game prototyping.
>> This is a well known series of tutorials for SDL:
>> http://lazyfoo.net/tutorials/SDL/02_getting_an_image_on_the_screen/index.php
>> These functions need to be called first:
>> SDL_Init( SDL_INIT_VIDEO );
>> SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED,
>> SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
>> SDL_GetWindowSurface( gWindow );
>> SDL_CreateWindow and SDL_GetWindowSurface return pointers to SDL objects.
>> This is my J attempt at calling these functions:
>> 
>> load 'dll files'
>> 
>> 
>> lib =: '/path/to/libSDL2.so'
>> 
>> NB. takes an int, returns a pointer
>> 
>> SDL_Init =: 'SDL_Init i i'
>> 
>> NB. takes window title, dimensions and returns a pointer
>> 
>> SDL_CreateWindow =: 'SDL_CreateWindow i *c i i i i i'
>> 
>> NB. takes the previous pointer and returns a pointer to a window surface.
>> 
>> SDL_GetWindowSurface =: 'SDL_GetWindowSurface i x'
>> 
>> NB. Load a bitmap, needs the path to the bitmap
>> 
>> SDL_LoadBMP =: 'SDL_LoadBMP i *c'
>> 
>> 
>> initParams =: <0
>> 
>> cwParams =: 'DEMO';10;10;700;500;0
>> 
>> bmpParams =: <'path/to/bmp file'
>> 
>> 
>> call1 =: lib, ' ',SDL_Init
>> 
>> call2 =: lib,' ',SDL_CreateWindow
>> 
>> call3 =: lib,' ',SDL_GetWindowSurface
>> 
>> 
>> call4 =: lib,' ', SDL_LoadBMP
>> 
>> 
>> 
>> 
>> Now I do
>> 
>> call1 cd initParams NB. seems ok after calling this
>> 
>> call2 cd cwParams NB. seems ok. Window should be created. On Mac / Linux it
>> created a black window
>> 
>> call3 cd 1292939 NB. the right argument is the previous result's returned
>> pointer.
>> 
>> 
>> The last call crashes J on Mac, but doesn't crash it on Linux (32-bit
>> Ubuntu).
>> 
>> So on Ubuntu I got this far, and it seems like it might just work, so I
>> want to try putting a bitmap on the screen. First I need to load the bitmap.
>> 
>> call4 cd bmpParams
>> 
>> This is where I always get a domain error. I am not sure why. It sould be
>> noted that you can't use PNG files with LoadBitmap, you need another
>> library (SDL_image).
>> 
>> Why call4 gives a domain error is difficult to understand. The path is
>> correct, and it SDL_LoadBMP looks like this:
>> 
>> https://wiki.libsdl.org/SDL_LoadBMP
>> 
>> Any ideas how to proceed? Thanks in advance. As I said, I am really just
>> experimenting so I do not know if this should work.
>> 
>> Thanks,
>> 
>> Jon
>> 
>> 
>> ----------------------------------------------------------------------
>> 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

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

Reply via email to