diff options
| -rw-r--r-- | main.c | 75 | 
1 files changed, 55 insertions, 20 deletions
| @@ -5,6 +5,15 @@  #define WINDOW_W 1280  #define WINDOW_H 720 +void renderMaskedTexture(SDL_Renderer *context, SDL_Texture *result, SDL_Texture *mask, SDL_Texture *diffuse) { +	SDL_SetRenderTarget(context, result); +	SDL_SetRenderDrawColor(context, 0, 0, 0, 0); +	SDL_RenderClear(context); +	SDL_RenderCopy(context, mask, 0, 0); +	SDL_RenderCopy(context, diffuse, 0, 0); +	SDL_SetRenderTarget(context, 0); +} +  SDL_Texture *createTiledTexture(SDL_Renderer *context, const char *filename) {  	SDL_Surface *texture_surface = SDL_LoadBMP(filename);  	SDL_Texture *texture_tile = SDL_CreateTextureFromSurface(context, texture_surface); @@ -14,7 +23,7 @@ SDL_Texture *createTiledTexture(SDL_Renderer *context, const char *filename) {  	SDL_Texture *texture = SDL_CreateTexture(context,  			SDL_PIXELFORMAT_RGBA8888, -			SDL_TEXTUREACCESS_STATIC | SDL_TEXTUREACCESS_TARGET, +			SDL_TEXTUREACCESS_TARGET,  			WINDOW_W, WINDOW_H);  	SDL_SetRenderTarget(context, texture);  	for (int y = 0; y < WINDOW_H; y += tile_h) { @@ -40,16 +49,20 @@ int main() {  	SDL_Renderer *context = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); -	SDL_SetRenderDrawColor(context, 40, 40, 40, 255); -	SDL_RenderClear(context); -  	SDL_Texture *background = createTiledTexture(context, "background.bmp"); +  	SDL_Texture *foreground = createTiledTexture(context, "foreground.bmp"); -	SDL_SetTextureBlendMode(foreground, SDL_BLENDMODE_BLEND); +	SDL_SetTextureBlendMode(foreground, SDL_BLENDMODE_MOD);  	SDL_Surface *mask_surface = SDL_CreateRGBSurfaceWithFormat(0,  			WINDOW_W, WINDOW_H, 32, SDL_PIXELFORMAT_RGBA8888); +	SDL_Texture *terrain = SDL_CreateTexture(context, +			SDL_PIXELFORMAT_RGBA8888, +			SDL_TEXTUREACCESS_TARGET, +			WINDOW_W, WINDOW_H); +	SDL_SetTextureBlendMode(terrain, SDL_BLENDMODE_BLEND); +  	SDL_LockSurface(mask_surface);  	for (int i = 0; i < mask_surface->w * mask_surface->h; i++) {  		((Uint32 *)mask_surface->pixels)[i] = 0x00000000; @@ -58,21 +71,9 @@ int main() {  		((Uint32 *)mask_surface->pixels)[i] = 0xffffffff;  	}  	SDL_UnlockSurface(mask_surface); +	SDL_Texture *mask_texture = SDL_CreateTextureFromSurface(context, mask_surface); -	SDL_Texture *terrain = SDL_CreateTextureFromSurface(context, mask_surface); - -	SDL_BlendMode true_mul = SDL_ComposeCustomBlendMode( -			SDL_BLENDFACTOR_ZERO, -			SDL_BLENDFACTOR_SRC_COLOR, -			SDL_BLENDOPERATION_ADD, -			SDL_BLENDFACTOR_ONE, -			SDL_BLENDFACTOR_ZERO, -			SDL_BLENDOPERATION_ADD); -	SDL_SetTextureBlendMode(terrain, true_mul); - -	SDL_SetRenderTarget(context, foreground); -	SDL_RenderCopy(context, terrain, 0, 0); -	SDL_SetRenderTarget(context, 0); +	renderMaskedTexture(context, terrain, mask_texture, foreground);  	int exit = 0;  	while (!exit) { @@ -82,8 +83,42 @@ int main() {  				exit = 1;  			}  		} +		int mouse_x, mouse_y; +		Uint32 mouse_state = SDL_GetMouseState(&mouse_x, &mouse_y); +		if (mouse_state & SDL_BUTTON_LMASK) { +			SDL_LockSurface(mask_surface); +			unsigned size = 100; +			for (int j = mouse_y - size / 2; j < mouse_y + size / 2; j++) { +				for (int i = mouse_x - size / 2; i < mouse_x + size / 2; i++) { +					size_t index = i + j * WINDOW_W; +					((Uint32 *)mask_surface->pixels)[index] = 0xffffffff; +				} +			} +			SDL_UnlockSurface(mask_surface); +			SDL_DestroyTexture(mask_texture); +			mask_texture = SDL_CreateTextureFromSurface(context, mask_surface); + +			renderMaskedTexture(context, terrain, mask_texture, foreground); +		} else if (mouse_state & SDL_BUTTON_RMASK) { +			SDL_LockSurface(mask_surface); +			unsigned size = 100; +			for (int j = mouse_y - size / 2; j < mouse_y + size / 2; j++) { +				for (int i = mouse_x - size / 2; i < mouse_x + size / 2; i++) { +					size_t index = i + j * WINDOW_W; +					((Uint32 *)mask_surface->pixels)[index] = 0x00000000; +				} +			} +			SDL_UnlockSurface(mask_surface); +			SDL_DestroyTexture(mask_texture); +			mask_texture = SDL_CreateTextureFromSurface(context, mask_surface); + +			renderMaskedTexture(context, terrain, mask_texture, foreground); +		} +		SDL_SetRenderDrawColor(context, 40, 40, 40, 255); +		SDL_RenderClear(context); +  		SDL_RenderCopy(context, background, 0, 0); -		SDL_RenderCopy(context, foreground, 0, 0); +		SDL_RenderCopy(context, terrain, 0, 0);  		SDL_RenderPresent(context);  		SDL_Delay(10); | 
