Help making a game with transparency

matheus matheus at gmail.com
Sat Sep 28 13:41:24 UTC 2019


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.


More information about the Digitalmars-d-learn mailing list