1. Help Solving SDL2 Wrapper Example [SOLVED]

Hello,

I have updated the EuSDL2 Wrapper for SDL 2.0.6 http://www.rapideuphoria.com/uploads/eusdl2.zip

I am still having a problem with the SDL_CreateRenderer() function however. The error I get is a machine-level exception. Bad routine number (-1).

-Wrapper Code

xSDL_CreateRenderer = define_c_func(sdl2,"SDL_CreateRender",{C_POINTER,C_INT,C_UINT},C_POINTER) 
 
public function SDL_CreateRenderer(atom win,integer idx,SDL_RENDERERFLAGS flags) 
 
 return c_func(xSDL_CreateRenderer,{win,idx,flags}) 
	 
end function 

-Example

include std/get.e 
include std/io.e 
include std/machine.e 
include EuSDL2.ew 
include flags.e 
 
if SDL_Init(SDL_INIT_EVERYTHING) = -1 then 
	puts(1,"Failed to init SDL2!\n") 
	abort(0) 
end if 
 
atom win = SDL_CreateWindow("Render Demo",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,640,480,SDL_WINDOW_SHOWN) 
atom ren = SDL_CreateRenderer(win,-1,SDL_RENDERER_ACCELERATED) 
 
 
atom gfx,tex 
 
gfx = SDL_LoadBMP("SDL.bmp") 
tex = SDL_CreateTextureFromSurface(ren,gfx) 
 
if win = -1 then 
	puts(1,"Failed to create window!\n") 
	abort(0) 
end if 
 
atom key = 0 
integer running = 1 
 
while running = 1 do 
	 
	SDL_PumpEvents() 
	 
	key = SDL_GetKeyboardState(key) 
	 
	if (peek(key+SDL_SCANCODE_ESCAPE) > 0) then 
			running = 0 
	end if 
	 
	--SDL_SetRenderDrawColor(ren,0,0,0,0) 
	 
	if SDL_RenderClear(ren) < 0 then 
		puts(1,"Failed to clear window!\n") 
		abort(0) 
	end if 
	 
	if SDL_RenderCopy(ren,tex,0,0) < 0 then 
		puts(1,"Failed to render image!\n") 
		abort(0) 
	end if 
	 
	SDL_RenderPresent(ren) 
 
end while 
 
SDL_FreeSurface(gfx) 
SDL_DestroyTexture(tex) 
SDL_DestroyRenderer(ren) 
SDL_DestroyWindow(win) 
 
SDL_Quit() 
new topic     » topic index » view message » categorize

2. Re: Help Solving SDL2 Wrapper Example

Icy_Viking said...
xSDL_CreateRenderer = define_c_func(sdl2,"SDL_CreateRender",{C_POINTER,C_INT,C_UINT},C_POINTER) 

Does this help?

xSDL_CreateRenderer = define_c_func(sdl2,"SDL_CreateRenderer",{C_POINTER,C_INT,C_UINT},C_POINTER) 
new topic     » goto parent     » topic index » view message » categorize

3. Re: Help Solving SDL2 Wrapper Example

petelomax said...
Icy_Viking said...
xSDL_CreateRenderer = define_c_func(sdl2,"SDL_CreateRender",{C_POINTER,C_INT,C_UINT},C_POINTER) 

Does this help?

xSDL_CreateRenderer = define_c_func(sdl2,"SDL_CreateRenderer",{C_POINTER,C_INT,C_UINT},C_POINTER) 

Yes it does. Thank you. The solution was so simple.

OK, need help with one more. I get the same error as before, but I don't see any typos in this one.

-Wrapper Code

xSDL_BlitSurface = define_c_func(sdl2,"SDL_BlitSurface",{C_POINTER,C_POINTER,C_POINTER,C_POINTER},C_INT) 
 
public function SDL_BlitSurface(atom src,atom srcr,atom dst,atom dstr) 
 
 return c_func(xSDL_BlitSurface,{src,srcr,dst,dstr}) 
	 
end function 

-Example

--Demo currently not working 
include std/machine.e 
include EuSDL2.ew 
include flags.e 
 
atom width = 800, height = 600 
atom buffer,format 
atom x 
x = SDL_Init(SDL_INIT_EVERYTHING) 
 
if x = -1 then 
	puts(1,"Failed to load initialize SDL!\n") 
	abort(0) 
end if 
 
atom win = SDL_CreateWindow("Window Image",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,width,height,SDL_WINDOW_SHOWN) 
 
if win = -1 then 
	puts(1,"Failed to create window!\n") 
	abort(0) 
end if 
 
atom surf = SDL_GetWindowSurface(win) 
 
atom img = SDL_LoadBMP("SDL.bmp") 
 
if img = -1 then 
	puts(1,"Failed to load image!\n") 
	abort(0) 
end if 
 
procedure calc_rect(atom rect,integer sx,integer sy,integer tx,integer ty) 
 
 poke4(rect,sx) 
 poke4(rect+2,sy) 
 poke4(rect+4,tx) 
 poke4(rect+6,ty) 
	 
end procedure 
 
atom srect = allocate(10) 
atom drect = allocate(10) 
 
calc_rect(srect,0,0,100,100) 
calc_rect(drect,0,0,100,100) 
 
format = peek4u(surf+4) 
buffer = SDL_ConvertSurface(surf,format,0) 
 
x = SDL_BlitSurface(img,srect,surf,drect) 
 
if x = 0 then 
	puts(1,"Failed to blit image!\n") 
	abort(0) 
end if 
 
SDL_UpdateWindowSurface(win) 
 
SDL_Delay(3000) 
 
SDL_FreeSurface(img) 
SDL_FreeSurface(surf) 
 
SDL_DestroyWindow(win) 
 
SDL_Quit() 
 
new topic     » goto parent     » topic index » view message » categorize

4. Re: Help Solving SDL2 Wrapper Example

OK, I downloaded eusdl2.zip and opened sdl2.dll with dependency walker, only to find there is no SDL_BlitSurface entry.

So I downloaded the source code and a quick search revealed, in SDL_surface.h, this little gem:

#define SDL_BlitSurface SDL_UpperBlit 

Hence I humbly suggest this may help:

xSDL_BlitSurface = define_c_func(sdl2,"SDL_UpperBlit",{C_POINTER,C_POINTER,C_POINTER,C_POINTER},C_INT)  

Pete

PS: a comment along the lines of --NB this is NOT a typo! seems warranted.

new topic     » goto parent     » topic index » view message » categorize

5. Re: Help Solving SDL2 Wrapper Example

petelomax said...

OK, I downloaded eusdl2.zip and opened sdl2.dll with dependency walker, only to find there is no SDL_BlitSurface entry.

So I downloaded the source code and a quick search revealed, in SDL_surface.h, this little gem:

#define SDL_BlitSurface SDL_UpperBlit 

Hence I humbly suggest this may help:

xSDL_BlitSurface = define_c_func(sdl2,"SDL_UpperBlit",{C_POINTER,C_POINTER,C_POINTER,C_POINTER},C_INT)  

Pete

PS: a comment along the lines of --NB this is NOT a typo! seems warranted.

Thanks Pete. I also noticed there was an UpperBlitScaled function as well. I think I'll wrap that as well. Probably keep BlitSurface as legacy code for now.

EDIT: Well I added in the UpperBlit functions, now I get this error.

 function SDL_RWFromFile()  
type_check failure, fname is 42840784  
    fname = 42840784 
    mode = {114'r',98'b'} 
    str = <no value> 
    str2 = <no value> 
    ret = <no value> 
 
in function SDL_LoadBMP()   
    fname = {83'S',68'D',76'L',46'.',98'b',109'm',112'p'} 
    str = 42840784 
    src (from inlined routine 'SDL_LoadBMP_RW' at 13) = <no value> 

public constant xSDL_RWFromFile = define_c_func(sdl2,"SDL_RWFromFile",{C_POINTER,C_POINTER},C_POINTER) 
 
public function SDL_RWFromFile(sequence fname,sequence mode) 
 
 atom str = allocate_string(fname,1) 
 atom str2 = allocate_string(mode,1) 
  
 atom ret = c_func(xSDL_RWFromFile,{str,str2}) 
  
 return ret 
	 
end function 
 
public constant xSDL_LoadBMP = define_c_func(sdl2,"SDL_LoadBMP",{C_POINTER},C_POINTER), 
xSDL_LoadBMP_RW = define_c_func(sdl2,"SDL_LoadBMP_RW",{C_POINTER,C_INT},C_POINTER) 
 
public function SDL_LoadBMP_RW(atom src,integer xfree) 
 
 return c_func(xSDL_LoadBMP_RW,{src,xfree}) 
	 
end function 
 
public function SDL_LoadBMP(sequence fname) 
 
 atom str = allocate_string(fname,1) 
 return SDL_LoadBMP_RW(SDL_RWFromFile(str,"rb"),1) 
	 
end function 
 
 
--Demo currently not working 
include std/machine.e 
include EuSDL2.ew 
include flags.e 
 
atom width = 800, height = 600 
atom buffer,format 
atom x 
x = SDL_Init(SDL_INIT_EVERYTHING) 
 
if x = -1 then 
	puts(1,"Failed to load initialize SDL!\n") 
	abort(0) 
end if 
 
atom win = SDL_CreateWindow("Window Image",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,width,height,SDL_WINDOW_SHOWN) 
 
if win = -1 then 
	puts(1,"Failed to create window!\n") 
	abort(0) 
end if 
 
atom surf = SDL_GetWindowSurface(win) 
 
atom img = SDL_LoadBMP("SDL.bmp") 
 
if img = -1 then 
	puts(1,"Failed to load image!\n") 
	abort(0) 
end if 
 
procedure calc_rect(atom rect,integer sx,integer sy,integer tx,integer ty) 
 
 poke4(rect,sx) 
 poke4(rect+2,sy) 
 poke4(rect+4,tx) 
 poke4(rect+6,ty) 
	 
end procedure 
 
atom srect = allocate(10) 
atom drect = allocate(10) 
 
calc_rect(srect,0,0,100,100) 
calc_rect(drect,0,0,100,100) 
 
format = peek4u(surf+4) 
buffer = SDL_ConvertSurface(surf,format,0) 
 
x = SDL_UpperBlit(img,srect,surf,drect) 
 
if x = 0 then 
	puts(1,"Failed to blit image!\n") 
	abort(0) 
end if 
 
SDL_UpdateWindowSurface(win) 
 
SDL_Delay(3000) 
 
SDL_FreeSurface(img) 
SDL_FreeSurface(surf) 
 
SDL_DestroyWindow(win) 
 
SDL_Quit() 
new topic     » goto parent     » topic index » view message » categorize

6. Re: Help Solving SDL2 Wrapper Example

That's a pretty basic error...

public function SDL_LoadBMP(sequence fname) 
 
-- atom str = allocate_string(fname,1) 
-- return SDL_LoadBMP_RW(SDL_RWFromFile(str,"rb"),1) 
 return SDL_LoadBMP_RW(SDL_RWFromFile(fname,"rb"),1) 
     
end function 

works fine here (on Phix) anyway.

new topic     » goto parent     » topic index » view message » categorize

7. Re: Help Solving SDL2 Wrapper Example

petelomax said...

That's a pretty basic error...

public function SDL_LoadBMP(sequence fname) 
 
-- atom str = allocate_string(fname,1) 
-- return SDL_LoadBMP_RW(SDL_RWFromFile(str,"rb"),1) 
 return SDL_LoadBMP_RW(SDL_RWFromFile(fname,"rb"),1) 
     
end function 

works fine here (on Phix) anyway.

OK I changed that. It fixed it, but I had to change some of the demo source code for it to work and show the image.

--Demo currently not working 
include std/machine.e 
include EuSDL2.ew 
include flags.e 
 
atom width = 800, height = 600 
atom buffer,format 
atom x 
x = SDL_Init(SDL_INIT_EVERYTHING) 
 
if x = -1 then 
	puts(1,"Failed to load initialize SDL!\n") 
	abort(0) 
end if 
 
atom win = SDL_CreateWindow("Window Image",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,width,height,SDL_WINDOW_SHOWN) 
 
if win = -1 then 
	puts(1,"Failed to create window!\n") 
	abort(0) 
end if 
 
atom surf = SDL_GetWindowSurface(win) 
 
atom img = SDL_LoadBMP("SDL.bmp") 
 
if img = -1 then 
	puts(1,"Failed to load image!\n") 
	abort(0) 
end if 
 
--calc rect doesen't seem to affect anything. 
procedure calc_rect(atom rect,integer sx,integer sy,integer tx,integer ty) 
 
 poke4(rect,sx) 
 poke4(rect+2,sy) 
 poke4(rect+4,tx) 
 poke4(rect+6,ty) 
	 
end procedure 
 
atom srect = allocate(10) 
atom drect = allocate(10) 
 
calc_rect(srect,0,0,100,100)  
calc_rect(drect,0,0,100,100) 
 
format = peek4u(surf+4) 
buffer = SDL_ConvertSurface(surf,format,0) 
 
x = SDL_UpperBlit(img,0,surf,0) --had to change the srcRect and dstRect to 0 for the image to show 
 
if x = -1 then 
	puts(1,"Failed to blit image!\n") 
	abort(0) 
end if 
 
integer running = 1 
 
atom key = 0 
 
while running = 1 do 
 
	SDL_PumpEvents() 
	key = SDL_GetKeyboardState(key) 
	 
	if peek(key+SDL_SCANCODE_ESCAPE) > 0 then 
		running = 0 
	end if 
	 
	SDL_UpdateWindowSurface(win) 
end while 
 
SDL_FreeSurface(img) 
SDL_FreeSurface(surf) 
 
SDL_DestroyWindow(win) 
 
SDL_Quit() 
new topic     » goto parent     » topic index » view message » categorize

8. Re: Help Solving SDL2 Wrapper Example

My bad, I'd been fixing things for Phix and forgot those you'd need, in particular points 5 and 6, and probably point 2.

1) enclose all "include std/*" with --/* and --*/, eg

--/* 
include std/machine.e 
--*/ 

2) both BasicEvent.exw and ColorWinDemo.exw contain "free(key)" which needs to be deleted.
3) insert "{} = " or similar before calls to SDL_UpdateWindowSurface, SDL_SetRenderDrawColor, SDL_RenderFillRect, and SDL_RenderClear
4) "format" is reserved in Phix, change atom format and all uses of it in WinImage.exw to (eg) atom fmt
5) also in WinImage.exw, change

procedure calc_rect(atom rect,integer sx,integer sy,integer tx,integer ty) 
 
 poke4(rect,sx) 
-- poke4(rect+2,sy) 
-- poke4(rect+4,tx) 
-- poke4(rect+6,ty) 
 poke4(rect+4,sy) 
 poke4(rect+8,tx) 
 poke4(rect+12,ty) 
     
end procedure 

6) and after x = SDL_BlitSurface(img,srect,surf,drect), change

--if x = 0 then 
if x != 0 then 

Pete

new topic     » goto parent     » topic index » view message » categorize

9. Re: Help Solving SDL2 Wrapper Example [SOLVED]

petelomax said...

My bad, I'd been fixing things for Phix and forgot those you'd need, in particular points 5 and 6, and probably point 2.

1) enclose all "include std/*" with --/* and --*/, eg

--/* 
include std/machine.e 
--*/ 

2) both BasicEvent.exw and ColorWinDemo.exw contain "free(key)" which needs to be deleted.
3) insert "{} = " or similar before calls to SDL_UpdateWindowSurface, SDL_SetRenderDrawColor, SDL_RenderFillRect, and SDL_RenderClear
4) "format" is reserved in Phix, change atom format and all uses of it in WinImage.exw to (eg) atom fmt
5) also in WinImage.exw, change

procedure calc_rect(atom rect,integer sx,integer sy,integer tx,integer ty) 
 
 poke4(rect,sx) 
-- poke4(rect+2,sy) 
-- poke4(rect+4,tx) 
-- poke4(rect+6,ty) 
 poke4(rect+4,sy) 
 poke4(rect+8,tx) 
 poke4(rect+12,ty) 
     
end procedure 

6) and after x = SDL_BlitSurface(img,srect,surf,drect), change

--if x = 0 then 
if x != 0 then 

Pete

Thanks Pete, it works. Here is the new code. Note, I didn't use the srect(source rectangle) on purpose, since this is just a simple example.

include std/machine.e 
include EuSDL2.ew 
include flags.e 
 
atom width = 800, height = 600 
atom buffer,format 
atom x 
x = SDL_Init(SDL_INIT_EVERYTHING) 
 
if x = -1 then 
	puts(1,"Failed to load initialize SDL!\n") 
	abort(0) 
end if 
 
atom win = SDL_CreateWindow("Window Image",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,width,height,SDL_WINDOW_SHOWN) 
 
if win = -1 then 
	puts(1,"Failed to create window!\n") 
	abort(0) 
end if 
 
atom surf = SDL_GetWindowSurface(win) 
 
atom img = SDL_LoadBMP("SDL.bmp") 
 
if img = -1 then 
	puts(1,"Failed to load image!\n") 
	abort(0) 
end if 
 
procedure calc_rect(atom rect,integer sx,integer sy,integer tx,integer ty) 
 
 poke4(rect,sx) 
 poke4(rect+4,sy) 
 poke4(rect+8,tx) 
 poke4(rect+12,ty) 
	 
end procedure 
 
--atom srect = allocate(10) 
atom drect = allocate(10) 
 
--calc_rect(srect,10,10,100,100) 
calc_rect(drect,width / 2,height / 2,100,100) 
 
--Use SDL_UpperBlit to draw to screen 
--As this replaces SDL_BlitSurface 
x = SDL_UpperBlit(img,0,surf,drect) 
 
if x = -1 then 
	puts(1,"Failed to blit image!\n") 
	abort(0) 
end if 
 
integer running = 1 
 
atom key = 0 
 
while running = 1 do 
 
	SDL_PumpEvents() 
	key = SDL_GetKeyboardState(key) 
	 
	if peek(key+SDL_SCANCODE_ESCAPE) > 0 then 
		running = 0 
	end if 
	 
	SDL_UpdateWindowSurface(win) 
end while 
 
SDL_FreeSurface(img) 
SDL_FreeSurface(surf) 
 
SDL_DestroyWindow(win) 
 
SDL_Quit() 
new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu