Topic: fortran-sdl2 package

It seems that the function SDL_CreateRGBSurfaceFrom is not available in the package,
From readme.md (visual studio) the function seems not to be bound (see line 326).
Can this be corrected so that I can use the function?

Re: fortran-sdl2 package

Our fortran-sdl2 package is a bit old.  I'll try to get an update posted soon.

Jeff Armstrong
Approximatrix, LLC

Re: fortran-sdl2 package

thanks Jeff

Re: fortran-sdl2 package

I've updated fortran-sdl2 to the lastest repo version, and it seems to be working fine.  It is using the latest SDL2 packages as well. 

One word of warning: the Fortran library name has changed, which could cause an existing project's linking step to fail.  The library flag is now:

-lfortran-sdl2

Let me know if there's any problem!

Jeff Armstrong
Approximatrix, LLC

Re: fortran-sdl2 package

I reinstalled fortran-sdl2.
I changed the library flag. in the file: sfpm>templates>readme_sdl.txt>flags.txt
I cannot test if it works, because I have another compilation error, namely: ambiguous references:
for example:
Error: Name 'glclearcolor' at (1) is an ambiguous reference to 'glclearcolor' from module 'opengl_gl'

I do not know how to solve this.
I uninstalled and reinstalled opengl, but it does not help.

Re: fortran-sdl2 package

Interesting, I didn't see this issue.  Did you also reinstall:

- Simple DirectMedia Layer
- SDL2_image
- SDL2_mixer
- SDL2_ttf

I'm not sure it would upgrade those automatically. 

You say you've "uninstalled and reinstalled opengl," but I'm not sure I understand what package you're talking about.  glclearcolor is available in the sdl2 module, and it doesn't require any other imports.  I'm not sure if you're calling it directly or not.

Jeff Armstrong
Approximatrix, LLC

Re: fortran-sdl2 package

The ambiguous problem ha been solved. I had to disable the module "open_gl" (package F03GL), because it conflicted with the module "sdl2" (thanks to the remark of jeff). 
However my problem still persists. The function SDL_CreateRGBSurfaceFrom remains not recognised. From readme.md (visual studio) the function still seems not to be bound (see line 326).

What I actually want is to save an opengl image to a file as a bitmap, jpg or another format. Perhaps somebody has a solution??

Re: fortran-sdl2 package

sdl_create_rgb_surface_from does seem to be bound.  Are you using the correct function call?

Jeff Armstrong
Approximatrix, LLC

Re: fortran-sdl2 package

thanks let me try.
Anyway strange call first time I see it like this.
I will let you know if it works.

Re: fortran-sdl2 package

dear Jeff,
Happy New year!
I seem to have the correct function calls: "sdl_create_rgb_surface_from" and "sdl_save_bmp".
The library flag reads "lfortran-sdl2".
However I still get undefined references. I hope you can help. A part of the *build status* looks like this:

==============================================================================
Generating Makefile... Okay
==============================================================================
Processing default resource
Generating atb_ex.dll.exe
C:/Users/csitt/AppData/Local/sfpm/64/lib\libfortran-sdl2.a(sdl2.o): In function `__sdl2_MOD_sdl_log_warn':
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
C:\Users\jeffr\Workspace\packages\fortran_sdl2\fortran-sdl2/src/sdl2.f90:587: undefined reference to `SDL_LogWarn'
C:/Users/csitt/AppData/Local/sfpm/64/lib\libfortran-sdl2.a(sdl2.o): In function `__sdl2_MOD_sdl_log_verbose':
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
C:\Users\jeffr\Workspace\packages\fortran_sdl2\fortran-sdl2/src/sdl2.f90:578: undefined reference to `SDL_LogVerbose'
C:/Users/csitt/AppData/Local/sfpm/64/lib\libfortran-sdl2.a(sdl2.o): In function `__sdl2_MOD_sdl_log_info':
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
C:\Users\jeffr\Workspace\packages\fortran_sdl2\fortran-sdl2/src/sdl2.f90:569: undefined reference to `SDL_LogInfo'
C:/Users/csitt/AppData/Local/sfpm/64/lib\libfortran-sdl2.a(sdl2.o): In function `__sdl2_MOD_sdl_log_error':
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
C:\Users\jeffr\Workspace\packages\fortran_sdl2\fortran-sdl2/src/sdl2.f90:560: undefined reference to `SDL_LogError'
C:/Users/csitt/AppData/Local/sfpm/64/lib\libfortran-sdl2.a(sdl2.o): In function `__sdl2_MOD_sdl_log_debug':

Re: fortran-sdl2 package

What exactly do you currently have in the Linker box of Compiler Flags in Project Options?  Having just "-lfortran-sdl2" is not sufficient.  What I had meant was that the previous library had been using the flag "-lSDL_fortran" I believe.  But you actually need the complete flag set to link the actual SDL libraries:

-lfortran-sdl2 -lSDK2.dll -lSDL2_image.dll -lSDL2_mixer.dll -lSDL2_ttf.dll
Jeff Armstrong
Approximatrix, LLC

Re: fortran-sdl2 package

As you probably are aware I am new to opengl and sld2
I managed to compile the software without errors.
when I run the programme it creates a bmp file, but it is empty.
I tried several things, but am not able to solve the problem.
I try to save a window of 750x500 pixels (iwidth x iheight) containing an 2D image of a FEM boundary with basepoints and numbering.

the routine that should do the job reads:

subroutine save_image2()

use opengl_glu
use opengl_glut
use common_window
use, intrinsic :: iso_fortran_env, only: i1 => int8, i8 => int64, r8 => real64,&
                  stderr => error_unit, stdout => output_unit
use :: sdl2
use :: sdl2_image
use :: SDL2_mixer
use :: SDL2_ttf
   
implicit none
type(c_ptr),dimension(0:iwidth*iheight*3) :: image
type(sdl_surface),pointer :: bitmap
integer :: idepth,ipitch,mm

call glReadPixels(0,0,iwidth,iheight,GL_RGB,GL_UNSIGNED_BYTE,image)
idepth=32
ipitch=4*iwidth
!ipitch=0
bitmap => SDL_Create_RGB_Surface_From(image,iwidth,iheight,idepth,ipitch,&
         int(z'000000ff',kind=i8),int(z'0000ff00',kind=i8),&
         int(z'00ff0000',kind=i8),int(z'ff000000',kind=i8))
mm=sdl_save_bmp(bitmap,"test.bmp")

endsubroutine save_image2


If I fill out ipitch = 0, I get the empty bmp file
if I fill out ipitch = 4*iwidth I get an error (see below)

At line 28 of file .\save_image2.f90
Fortran runtime error: Pointer actual argument 'bitmap' is not associated
Error termination.

The window sizes iwidth and iheight are obtained via the common block "common_window"

I hope I provided enough information

Re: fortran-sdl2 package

The runtime error is stating that the variable bitmap is not associated, meaning that the call to SDL_Create_RGB_Surface_From has failed and returned a null pointer. 

One issue I can see is that pulling the pixels from OpenGL, you've requested the format as GL_RGB, which will not include an alpha channel.  Your SDL call, though, assumes an alpha channel is present, hence the 4 bytes per pixel.  You probably want to use GL_RGBA on the call to glReadPixels

Additionally, your call to glReadPixels is using a problematic pointer for image data.  The function definition is actually expecting a type(c_ptr) that points to a region of memory that can store the image.  What you really should have here instead is a Fortran pointer to allocated memory. 

The declaration should be simply:

integer(kind=GLbyte), dimension(:), pointer::image

Next, before using it for anything, you need to allocate it to the image size:

allocate(image(iwidth*iheight*4))  ! assumes 4 bytes (RGB + alpha per pixel)

Finally, on your OpenGL call, you need to provide the C location of the array:

call glReadPixels(0,0,iwidth,iheight,GL_RGBA,GL_UNSIGNED_BYTE,C_LOC(image))

Likewise, on the call to SDL:

bitmap => SDL_Create_RGB_Surface_From(C_LOC(image),iwidth,iheight,idepth,ipitch,&
         int(z'000000ff',kind=i8),int(z'0000ff00',kind=i8),&
         int(z'00ff0000',kind=i8),int(z'ff000000',kind=i8))

I haven't run the code, though.  Perhaps that will improve things.

Jeff Armstrong
Approximatrix, LLC

Re: fortran-sdl2 package

I adapted the code, it looks as follows:

subroutine save_image2()

use opengl_glu
use opengl_glut
use common_window
use, intrinsic :: iso_fortran_env, only: i1 => int8, i8 => int64, r8 => real64,&
                  stderr => error_unit, stdout => output_unit
use :: sdl2
use :: sdl2_image
use :: SDL2_mixer
use :: SDL2_ttf
   
implicit none
integer(kind=GLbyte), dimension(:), pointer::image
type(sdl_surface),pointer :: bitmap
integer :: idepth,ipitch,mm
allocate(image(iwidth*iheight*4))  ! assumes 4 bytes (RGB + alpha per pixel)

call glReadPixels(0,0,iwidth,iheight,GL_RGBA,GL_UNSIGNED_BYTE,c_loc(image))
idepth=32
ipitch=4*iwidth
!ipitch=0
bitmap => SDL_Create_RGB_Surface_From(c_loc(image),iwidth,iheight,idepth,&
   ipitch, int(z'000000ff',kind=i8),int(z'0000ff00',kind=i8),&
           int(z'00ff0000',kind=i8),int(z'ff000000',kind=i8))
       
mm=sdl_save_bmp(bitmap,"test.bmp")

endsubroutine save_image2


However I still get the same error:
At line 27 of file .\save_image2.f90
Fortran runtime error: Pointer actual argument 'bitmap' is not associated
Error termination.

Can it be that bitmap is a "sdl_surface pointer" and sdl_save_bmp requires a "sdl_surface"??

If I fill out ipitch=0 the code runs, but the saved file is empty

Re: fortran-sdl2 package

csitters wrote:

Can it be that bitmap is a "sdl_surface pointer" and sdl_save_bmp requires a "sdl_surface"??

No, the runtime library is reporting that bitmap is not associated, meaning the initial call to SDL_Create_RGB_Surface_From failed, returning a null (or, in Fortran, unassociated) pointer.

No, the actual problem here is that the fortran-sdl2 library contains a bug.  The Fortran binding for this function is incorrect.  If you look at the GitHub version of the Fortran interface, you'll notice that they've bound the C function not to SDL_CreateRGBSurfaceFrom, but, rather, SDL_CreateRGBSurface.  It appears to be a copypasta error.

I've already submitted a pull request to correct the issue.  In the meantime, I'll try to build a new version of the package to fix this.

Jeff Armstrong
Approximatrix, LLC

Re: fortran-sdl2 package

The version in the SF Package Manager has been updated to correct the issue.  Hopefully your code works now.

Jeff Armstrong
Approximatrix, LLC

Re: fortran-sdl2 package

Thanks, the software is working now.
I only need to solve the problem that the figure is shown upside down and mirror imaged.