aboutsummaryrefslogtreecommitdiff
path: root/src/layer_image.cpp
blob: 7f338b267da9b25539e7ffaaf7fe11cd5e1fde6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <math.h>

#include <SDL.h>
#include <SDL_image.h>
#include <SDL_rotozoom.h>

#include "osc_graphics.h"
#include "layer_image.h"

Layer::CtorInfo LayerImage::ctor_info = {"image", "s" /* file */};

LayerImage::LayerImage(const char *name, SDL_Rect geo, float opacity,
		       const char *file) :
		      Layer(name),
		      surf_alpha(NULL), surf_scaled(NULL), surf(NULL)
{
	file_osc_id = register_method("file", "s",
				      (OSCServer::MethodHandlerCb)file_osc);

	LayerImage::alpha(opacity);
	LayerImage::geo(geo);
	LayerImage::file(file);
}

void
LayerImage::geo(SDL_Rect geo)
{
	if (!geo.x && !geo.y && !geo.w && !geo.h)
		geov = (SDL_Rect){0, 0, screen->w, screen->h};
	else
		geov = geo;

	if (!surf)
		return;

	if (surf_scaled &&
	    surf_scaled->w == geov.w && surf_scaled->h == geov.h)
		return;

	SDL_FREESURFACE_SAFE(surf_alpha);
	SDL_FREESURFACE_SAFE(surf_scaled);

	if (surf->w != geov.w || surf->h != geov.h) {
		surf_scaled = zoomSurface(surf,
					  (double)geov.w/surf->w,
					  (double)geov.h/surf->h,
					  SMOOTHING_ON);
	}

	alpha(alphav);
}

void
LayerImage::alpha(float opacity)
{
	SDL_Surface *use_surf = surf_scaled ? : surf;
	Uint8 alpha = (Uint8)ceilf(opacity*SDL_ALPHA_OPAQUE);

	alphav = opacity;

	if (!use_surf)
		return;

	if (!use_surf->format->Amask) {
		if (alpha == SDL_ALPHA_OPAQUE)
			SDL_SetAlpha(use_surf, 0, 0);
		else
			SDL_SetAlpha(use_surf, SDL_SRCALPHA | SDL_RLEACCEL, alpha);

		return;
	}

	if (alpha == SDL_ALPHA_OPAQUE) {
		SDL_FREESURFACE_SAFE(surf_alpha);
		return;
	}

	if (!surf_alpha) {
		surf_alpha = SDL_CreateRGBSurface(use_surf->flags,
						  use_surf->w, use_surf->h,
						  use_surf->format->BitsPerPixel,
						  use_surf->format->Rmask,
						  use_surf->format->Gmask,
						  use_surf->format->Bmask,
						  use_surf->format->Amask);
	}

	rgba_blit_with_alpha(use_surf, surf_alpha, alpha);
}

void
LayerImage::file(const char *file)
{
	SDL_FREESURFACE_SAFE(surf_alpha);
	SDL_FREESURFACE_SAFE(surf_scaled);
	SDL_FREESURFACE_SAFE(surf);

	if (!file || !*file)
		return;

	surf = IMG_Load(file);
	if (!surf) {
		SDL_IMAGE_ERROR("IMG_Load");
		exit(EXIT_FAILURE);
	}

	geo(geov);
}

void
LayerImage::frame(SDL_Surface *target)
{
	if (surf)
		SDL_BlitSurface(surf_alpha ? : surf_scaled ? : surf, NULL,
				target, &geov);
}

LayerImage::~LayerImage()
{
	unregister_method(file_osc_id);

	SDL_FREESURFACE_SAFE(surf_alpha);
	SDL_FREESURFACE_SAFE(surf_scaled);
	SDL_FREESURFACE_SAFE(surf);
}