www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Help making a game with transparency

reply Murilo <murilomiranda92 hotmail.com> writes:
Hi guys, I am making a game but for some reason the sprites do 
not show with the transparent background that they were supposed 
to. I'm using the arsd library. Can anyone help me?
Sep 26 2019
next sibling parent reply matheus <matheus gmail.com> writes:
On Friday, 27 September 2019 at 02:54:27 UTC, Murilo wrote:
 Hi guys, I am making a game but for some reason the sprites do 
 not show with the transparent background that they were 
 supposed to. I'm using the arsd library. Can anyone help me?
Sorry this is a bit vague. I suppose you're using engine.d or screen.d directly right? Depending on your setup (OpenGL or Software) transparency will be different. For example take a look at line 733, putpixel function and you'll see that It handle Color differently if it's OpenGL x Software and for the latter it checks if 32bpp or less. Now if it's OpenGL take a look for "Alpha". Matheus.
Sep 27 2019
next sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Friday, 27 September 2019 at 11:28:35 UTC, matheus wrote:
 Sorry this is a bit vague. I suppose you're using engine.d or 
 screen.d directly right?
those two are obsolete, new stuff should use simpledisplay but with simpledisplay you need to use opengl for transparency since the xlib/gdi+ impl doesn't support it (though they probably could if someone wanted to write some code) i just am doing 4 other things at once right now and can't do it myself at the moment
Sep 27 2019
next sibling parent Murilo <murilomiranda92 hotmail.com> writes:
On Friday, 27 September 2019 at 11:32:53 UTC, Adam D. Ruppe wrote:
 On Friday, 27 September 2019 at 11:28:35 UTC, matheus wrote:
 Sorry this is a bit vague. I suppose you're using engine.d or 
 screen.d directly right?
those two are obsolete, new stuff should use simpledisplay but with simpledisplay you need to use opengl for transparency since the xlib/gdi+ impl doesn't support it (though they probably could if someone wanted to write some code) i just am doing 4 other things at once right now and can't do it myself at the moment
Alright, thanks, the problem is that I was unable to figure out the opengl functions. Later when you have the time see if you can help me out, then I will add this to your library's tutorial.
Sep 27 2019
prev sibling parent reply GreatSam4sure <greatsam4sure gmail.com> writes:
On Friday, 27 September 2019 at 11:32:53 UTC, Adam D. Ruppe wrote:
 On Friday, 27 September 2019 at 11:28:35 UTC, matheus wrote:
 Sorry this is a bit vague. I suppose you're using engine.d or 
 screen.d directly right?
those two are obsolete, new stuff should use simpledisplay but with simpledisplay you need to use opengl for transparency since the xlib/gdi+ impl doesn't support it (though they probably could if someone wanted to write some code) i just am doing 4 other things at once right now and can't do it myself at the moment
The last time I try this library on windows 10 it throws errors. Have you try it recently on windows I want to know can the windows be customized and does it support styling like JavaFX
Sep 28 2019
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Saturday, 28 September 2019 at 08:06:02 UTC, GreatSam4sure 
wrote:
 The last time I try this library on windows 10 it throws 
 errors. Have you try it recently on windows
What errors? I sometimes break it with careless pushes to master but it usually works for me.
 I want to know can the windows be customized and does it 
 support styling like JavaFX
simpledisplay creates a blank window, what goes in it is entirely up to you (you can even do a flag to skip the titlebar if you want, though I recommend against that generally as I find it user-hostile). There's three ways to draw in it: 1) the basic functions ScreenPainter provides (drawImage, drawRectangle, drawLine, etc), none of which do alpha blending and probably never will (though they might do mask-based single-color transparent pixels at some point when I get around to it or if someone sends me a PR). Nothing fancy here. 2) OpenGL functions. The simplewindow constructor can create a context for you for various opengl versions. Old style glBegin/glEnd is the default and has prototypes included inline. Newer style stuff is opt-in and you may need to load functions via an additional library (for example, the nanovega.d lib in there does this and includes its own vector graphics API, including alpha blending btw) or by dynamically loading them yourself (all the pieces are in simpledisplay it just doesn't do it all by default). These can get as fancy as you want. 3) Operating system functions. You can get at the window id/handle via the impl member of the window and hook the native event loop, call native functions, etc. to your heart's desire. If you do something that is generally useful and can be done at reasonable cost on X11 (Windows and Macintosh are both quite capable, it is usually X that ends up being the lowest common denominator), send me a PR and I might add it to screenpainter's api. If you're looking for UI widgets like buttons and such, again you can DIY or my minigui.d contains a bunch of them. But they only contain the ones I have personally used so far and only do the styles I personally like and can implement cheaply and easily. There's no fancy CSS or animations or anything like that. I sometimes consider it but I'm personally pretty meh on them so don't expect it any time soon. But if you all want that, on your schedule, smash that like button, comment, subscribe, and be sure to ring that bell so you get a notification every time I parrot what people say on Youtube at the end of their videos. Then head on over my patreon page and give me all your money today. If everyone who downloaded my libraries contributed just $3 / month, I'd be able to devote myself to expanding these libraries to support more use cases. But until then, I write these for myself (or, in rare cases, someone else writes them for me and contributes it like with nanovega) so the functionality tend to focus on what I have used myself in the past. Then I post them online since it is easy to do so... then if you find them useful, great, and if you don't, oh well, there's always SDL or gtkD or html5 in electron (LOL) to cover more cases.
Sep 28 2019
next sibling parent reply Murilo <murilomiranda92 hotmail.com> writes:
On Saturday, 28 September 2019 at 14:33:03 UTC, Adam D. Ruppe 
wrote:
 But if you all want that, on your schedule, smash that like 
 button, comment, subscribe, and be sure to ring that bell so 
 you get a notification every time I parrot what people say on 
 Youtube at the end of their videos. Then head on over my 
 patreon page and give me all your money today. If everyone who 
 downloaded my libraries contributed just $3 / month, I'd be 
 able to devote myself to expanding these libraries to support 
 more use cases.
What are your pages that you want people subscribing to? And what is your patreon page? I may buy your book "D cookbook" next year, that will probably be a good contribution.
Sep 30 2019
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Monday, 30 September 2019 at 20:14:56 UTC, Murilo wrote:
 What are your pages that you want people subscribing to?
I'm just trolling.
 And what is your patreon page?
https://www.patreon.com/adam_d_ruppe but as you'll notice, it is currently $58. To reach "quit my job and work for y'all all the time" I put $8000. Realistically, that is never going to happen. thus why i troll.
Oct 01 2019
parent Murilo <murilomiranda92 hotmail.com> writes:
On Tuesday, 1 October 2019 at 12:46:58 UTC, Adam D. Ruppe wrote:
 On Monday, 30 September 2019 at 20:14:56 UTC, Murilo wrote:
 What are your pages that you want people subscribing to?
I'm just trolling.
 And what is your patreon page?
https://www.patreon.com/adam_d_ruppe but as you'll notice, it is currently $58. To reach "quit my job and work for y'all all the time" I put $8000. Realistically, that is never going to happen. thus why i troll.
Alright, sending you money every month on Patreon would be a problem for me so I figure I can patron you in another way, I am writing a tutorial for your library and I have showed lots of people how useful your library can be, I have also sent several PR to add stuff to it. That will be my way of contributing.
Oct 01 2019
prev sibling parent reply Murilo <murilomiranda92 hotmail.com> writes:
Hi everyone, I tried creating a program which simply shows the 
image of the ship near the corner of the screen, just for testing 
sake. Here it is:

import arsd.gamehelpers, arsd.image;
​
void main()​
{​
     SimpleWindow window = create2dWindow("Space Invaders", 1000, 
400);​
     OpenGlTexture ship = new 
OpenGlTexture(loadImageFromFile("images/ship_normal.png").getAsTrueColorImage());​
     window.eventLoop(40,​
     (/*no events*/)​
     {​
         window.redrawOpenGlSceneNow;​
         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | 
GL_ACCUM_BUFFER_BIT);​
         glLoadIdentity;​
         ship.draw(20, 20, 41, 47);​
     });​
}​

But the screen stays all white, nothing appears in it. Any ideas 
what needs to be changed?
Sep 30 2019
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Monday, 30 September 2019 at 23:52:27 UTC, Murilo wrote:
     {​
         window.redrawOpenGlSceneNow;​
like I said on email, this is the ONLY thing you should to in the event loop to trigger the redraw.
         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | 
 GL_ACCUM_BUFFER_BIT);​
         glLoadIdentity;​
         ship.draw(20, 20, 41, 47);​
All this actual drawing stuff should be attached to the `window.redrawOpenGlScene` delegate. Just do `window.redrawOpenGlScene = { that stuff } ` after creating the window but before the event loop to set it up.
Oct 01 2019
parent Murilo <murilomiranda92 hotmail.com> writes:
On Tuesday, 1 October 2019 at 12:45:35 UTC, Adam D. Ruppe wrote:
 On Monday, 30 September 2019 at 23:52:27 UTC, Murilo wrote:
     {​
         window.redrawOpenGlSceneNow;​
like I said on email, this is the ONLY thing you should to in the event loop to trigger the redraw.
         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | 
 GL_ACCUM_BUFFER_BIT);​
         glLoadIdentity;​
         ship.draw(20, 20, 41, 47);​
All this actual drawing stuff should be attached to the `window.redrawOpenGlScene` delegate. Just do `window.redrawOpenGlScene = { that stuff } ` after creating the window but before the event loop to set it up.
It worked! :D Thank you so much man. Now I will finish refactoring the code and then I will be able to finish your tutorial.
Oct 01 2019
prev sibling parent reply Murilo <murilomiranda92 hotmail.com> writes:
 Sorry this is a bit vague. I suppose you're using engine.d or 
 screen.d directly right?

 Depending on your setup (OpenGL or Software) transparency will 
 be different.

 For example take a look at line 733, putpixel function and 
 you'll see that It handle Color differently if it's OpenGL x 
 Software and for the latter it checks if 32bpp or less.

 Now if it's OpenGL take a look for "Alpha".

 Matheus.
Thanks for the reply. Do you know the arsd library?
Sep 27 2019
parent reply matheus gmail.com <matheus gmail.com> writes:
On Friday, 27 September 2019 at 16:36:14 UTC, Murilo wrote:
 ...Do you know the arsd library?
Yes but I use mostly terminal.d and others. On the other hand I use to code games too using SDL and OpenGL. I know for example in OpenGL you can do: glEnable(GL_ALPHA_TEST); to enable alpha channel and set transparency. Matheus.
Sep 27 2019
parent reply Murilo <murilomiranda92 hotmail.com> writes:
On Friday, 27 September 2019 at 17:53:33 UTC, matheus wrote:
 On Friday, 27 September 2019 at 16:36:14 UTC, Murilo wrote:
 ...Do you know the arsd library?
Yes but I use mostly terminal.d and others. On the other hand I use to code games too using SDL and OpenGL. I know for example in OpenGL you can do: glEnable(GL_ALPHA_TEST); to enable alpha channel and set transparency. Matheus.
Thanks, but I don't know how that will fit in my code. I will show up a code snippet and you tell me how I can use your idea in it, okay? import arsd.image, arsd.simpledisplay; void main() { auto memImgShip = loadImageFromFile("ship.png"), memImgBackground = loadImageFromFile("background.png"); auto imgShip = Image.fromMemoryImage(memImgShip), imgBackground = Image.fromMemoryImage(memImgBackground); auto window = new SimpleWindow; window.eventLoop(10, { auto painter = window.draw; painter.drawImage(Point(0, 0), imgBackground); painter.drawImage(Point(100, 100), imgShip); }); } Here it is, how do I make the ship have a transparent background?
Sep 27 2019
parent reply matheus <matheus gmail.com> writes:
On Friday, 27 September 2019 at 21:16:07 UTC, Murilo wrote:
 ...
 Here it is, how do I make the ship have a transparent 
 background?
First: Your PNG file has transparency data information right? Second: I was Looking into the drawImage function (Line 854): https://github.com/adamdruppe/arsd/blob/b0d21de148ef0b23ea845be322af5e6931ca4cb6/screen.d And I'd suggest you to try to add the glEnable(GL_ALPHA_TEST); before glBegin(GL_QUADS); In fact you should do this only once, maybe inside the constructor, but just try it there to see if it works. Matheus.
Sep 27 2019
next sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Friday, 27 September 2019 at 22:13:43 UTC, matheus wrote:
 https://github.com/adamdruppe/arsd/blob/b0d21de148ef0b23ea845be322af5e6931ca4cb6/screen.d
I really should just remove that file as it is no longer well maintained. I haven't used it for anything in years and doubt anyone else is either. He's using the simpledisplay.d lib which DOES NOT SUPPORT transparency in its drawImage function. It does NOT use opengl functions and are not compatible with them. To do opengl stuff with simpledisplay, there is a separate flow. You use opengl functions on a redraw scene delegate instead of using any of the Image or Painter objects. It is quite different and there is no easy fix on that end. but the gdi+ functions in sdpy MIGHT be able to be ported to alpha blend on Windows easily enough. I just haven't gotten around to it yet.
Sep 27 2019
parent reply Murilo <murilomiranda92 hotmail.com> writes:
On Friday, 27 September 2019 at 22:40:14 UTC, Adam D. Ruppe wrote:
 On Friday, 27 September 2019 at 22:13:43 UTC, matheus wrote:
 https://github.com/adamdruppe/arsd/blob/b0d21de148ef0b23ea845be322af5e6931ca4cb6/screen.d
I really should just remove that file as it is no longer well maintained. I haven't used it for anything in years and doubt anyone else is either. He's using the simpledisplay.d lib which DOES NOT SUPPORT transparency in its drawImage function. It does NOT use opengl functions and are not compatible with them. To do opengl stuff with simpledisplay, there is a separate flow. You use opengl functions on a redraw scene delegate instead of using any of the Image or Painter objects. It is quite different and there is no easy fix on that end. but the gdi+ functions in sdpy MIGHT be able to be ported to alpha blend on Windows easily enough. I just haven't gotten around to it yet.
Ahhh, that clears everything up. I will then leave the program without the transparency and wait until you get around to implement the fixes to it, I am not a developer, I am a scientist, I only use libraries, I don't know how to make them properly.
Sep 27 2019
parent reply Mike Parker <aldacron gmail.com> writes:
On Friday, 27 September 2019 at 22:55:22 UTC, Murilo wrote:

 Ahhh, that clears everything up. I will then leave the program 
 without the transparency and wait until you get around to 
 implement the fixes to it, I am not a developer, I am a 
 scientist, I only use libraries, I don't know how to make them 
 properly.
You might consider using SDL and SDL_image: https://libsdl.org/download-2.0.php https://www.libsdl.org/projects/SDL_image/ D bindings for both are available here: https://code.dlang.org/packages/bindbc-sdl You don't actually *need* SDL_image, as core SDL can load images in the BITMAP format, and you can then use the API to create transparency from a specific color. But if you want to load PNG files with alpha, you'll want SDL_image. There are numerous tutorials online for SDL, though if you're searching, make sure to look for SDL 2 tutorials and ignore anything for SDL 1.x. The tutorials will all be in either C or C++, but the SDL API using the D bindings is the same. The Lazy Foo tutorials are often recommended: https://lazyfoo.net/tutorials/SDL/ But again, these are C++, so you have to consider the differences in D and C++ when porting the code over. For example, he uses class destructors to free resources. Do that in D and you'll run into trouble. So just ignore how he structures his programs and focus on the main concepts of using SDL: how to acquire/release resources, how to render, etc. You can adapt that to any architecture you want. There's no need for classes at all, for example. Some tutorials out there will show you how to use SDL with OpenGL. Please ignore those. SDL's 2D Rendering API is what you want when you're learning, so focus on tutorials tat show you how to use it. The SDL Wiki has API documentation: http://wiki.libsdl.org/APIByCategory
Sep 27 2019
parent Murilo <murilomiranda92 hotmail.com> writes:
On Saturday, 28 September 2019 at 05:57:55 UTC, Mike Parker wrote:
 On Friday, 27 September 2019 at 22:55:22 UTC, Murilo wrote:
 You might consider using SDL and SDL_image:
Thank you very much. But I want to use only the arsd library.
Sep 28 2019
prev sibling parent Murilo <murilomiranda92 hotmail.com> writes:
On Friday, 27 September 2019 at 22:13:43 UTC, matheus wrote:
 On Friday, 27 September 2019 at 21:16:07 UTC, Murilo wrote:
 ...
 Here it is, how do I make the ship have a transparent 
 background?
First: Your PNG file has transparency data information right? Second: I was Looking into the drawImage function (Line 854): https://github.com/adamdruppe/arsd/blob/b0d21de148ef0b23ea845be322af5e6931ca4cb6/screen.d And I'd suggest you to try to add the glEnable(GL_ALPHA_TEST); before glBegin(GL_QUADS); In fact you should do this only once, maybe inside the constructor, but just try it there to see if it works. Matheus.
Thanks for trying to help me but unfortunately you are suggesting I use arsd.screen which is supposed to be obsolete, I am using arsd.simpledisplay instead and it is very different although many functions have the same name.
Sep 27 2019
prev sibling parent reply matheus <matheus gmail.com> writes:
Ok, I took a look over my old projects and I found exactly what 
you want, by the way it's from 2012.

It uses Derelict 2.0 bindings and will draw a PNG image where you 
can move around with cursor keys.

If you want I can send you the whole project (Makefile, DLL's) 
and everything else to build it.

Code:

/* 02/10/2012 */
import std.stdio;
import derelict.sdl.sdl;
import derelict.sdl.image;

pragma(lib, "DerelictSDL.lib");
pragma(lib, "DerelictUtil.lib");
pragma(lib, "DerelictSDLImage.lib");

void main(){
	// Screen width, height and bit per pixel AKA color
	int width = 600, height = 480, bpp = 24;
	short xPos, yPos;

	// Load Derelict SDL bindings
	DerelictSDL.load();
	DerelictSDLImage.load();

	SDL_Surface* screen;
	SDL_Rect rScreen;

	// Pointing to an Array of the current key state
	Uint8 *keystate = SDL_GetKeyState(null);
		
	// Try initiate SDL
     	if (SDL_Init(SDL_INIT_VIDEO) < 0){
		throw new Exception("Failed: Can not initialize SDL!");
     	}

	screen = SDL_SetVideoMode(width, height, bpp, SDL_ANYFORMAT);// 
SDL_HWSURFACE);

	IMG_Init(IMG_INIT_PNG);
	auto img = IMG_Load("caco.png");
	if(img is null){
		throw new Exception("Image not found!");
	}

	writeln(" - w: ",img.w, " - h: ", img.h, " - bpp:", 
img.format.BitsPerPixel);

	if (screen is null){
		throw new Exception("Failed: Can not set video! ");
     	}

	SDL_WM_SetCaption("Simple Example", null);

	xPos = cast(short)(width   / 2 - 128);
	yPos = cast(short)(height  / 2 - 128);

	// Game loop
	while(!keystate[SDLK_ESCAPE]){
		SDL_Delay(2); // To not stress CPU

		// Fill screen with RED
		SDL_FillRect(screen, null, 0xFF0000);

		// Update key state array
		SDL_PumpEvents();

		// User's control
		yPos += keystate[SDLK_DOWN]-keystate[SDLK_UP];
		xPos += keystate[SDLK_RIGHT]-keystate[SDLK_LEFT];

                 // Where to plot the image on the screen
		rScreen.x = xPos;
		rScreen.y = yPos;
		rScreen.w = rScreen.h = 256;
		
		SDL_BlitSurface(img,null,screen,&rScreen); // Draw Image
		SDL_Flip(screen); // Update Screen
	}
	IMG_Quit();
	SDL_FreeSurface(img);
	SDL_Quit();
}


Matheus.
Sep 28 2019
next sibling parent Mike Parker <aldacron gmail.com> writes:
On Saturday, 28 September 2019 at 13:41:24 UTC, matheus wrote:
 Ok, I took a look over my old projects and I found exactly what 
 you want, by the way it's from 2012.

 It uses Derelict 2.0 bindings and will draw a PNG image where 
 you can move around with cursor keys.
Murilo, if you do decide to use this example as a basis for your project, please don't use DerelictSDL2. I'm not maintaining it anymore. bindbc-sdl, which I linked in my previous post, is what you should prefer. The API to load the library and handle errors is different, but the SDL API calls will all be the same.
Sep 28 2019
prev sibling parent Murilo <murilomiranda92 hotmail.com> writes:
On Saturday, 28 September 2019 at 13:41:24 UTC, matheus wrote:
 Ok, I took a look over my old projects and I found exactly what 
 you want, by the way it's from 2012.

 It uses Derelict 2.0 bindings and will draw a PNG image where 
 you can move around with cursor keys.

 If you want I can send you the whole project (Makefile, DLL's) 
 and everything else to build it.

 Code:

 /* 02/10/2012 */
 import std.stdio;
 import derelict.sdl.sdl;
 import derelict.sdl.image;
 Matheus.
Thank you very much.
Sep 30 2019