/
Img.cpp
115 lines (94 loc) · 2.79 KB
/
Img.cpp
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
#include "StdAfx.h"
#include "Img.h"
Uint32 get_pixel32( SDL_Surface *surface, int x, int y )
{
//Convert the pixels to 32 bit
Uint32 *pixels = (Uint32 *)surface->pixels;
//Get the requested pixel
return pixels[ ( y * surface->w ) + x ];
}
void put_pixel32( SDL_Surface *surface, int x, int y, Uint32 pixel )
{
//Convert the pixels to 32 bit
Uint32 *pixels = (Uint32 *)surface->pixels;
//Set the pixel
pixels[ ( y * surface->w ) + x ] = pixel;
}
int CImg::getWidth() {
return width;
}
int CImg::getHeight() {
return height;
}
CImg::CImg(char *filename)
{
image = IMG_Load(filename);
if (image == NULL) {
printf("Couldn't load %s: %s\n", filename, SDL_GetError());
}
int format = SDL_PIXELFORMAT_ABGR8888; /* desired texture format */
texture_id = SDL_CreateTextureFromSurface(format, image);
if (texture_id == 0) {
printf("problem with texture: %s",SDL_GetError());
}
SDL_SetTextureBlendMode(texture_id, SDL_TEXTUREBLENDMODE_BLEND);
width = image->w;
height = image->h;
SDL_FreeSurface(image);
}
CImg::~CImg(void)
{
}
void CImg::InitWithPixels(void *pixels) {
}
void CImg::Init(char *filename) {
image = IMG_Load(filename);
if (image == NULL) {
printf("Couldn't load %s: %s\n", filename, SDL_GetError());
}
int format = SDL_PIXELFORMAT_ABGR8888; /* desired texture format */
texture_id = SDL_CreateTextureFromSurface(format, image);
if (texture_id == 0) {
printf("problem with texture: %s",SDL_GetError());
}
SDL_SetTextureBlendMode(texture_id, SDL_TEXTUREBLENDMODE_BLEND);
}
void CImg::Render(int x, int y,int alpha) {
//SDL_SetAlpha(image, SDL_SRCALPHA, alpha);
dst.x = x;
dst.y = y;
// SDL_BlitSurface(image, NULL, screen, &dst);
dst.w = width;
dst.h = height;
SDL_SetTextureAlphaMod(texture_id,(Uint8)alpha);
SDL_RenderCopy(texture_id,0,&dst);
}
void CImg::ChangeColor(int r, int g, int b) {
SDL_SetTextureColorMod(texture_id,r,g,b);
}
void CImg::RenderColor(SDL_Rect *fdst, int x, int y,int alpha, int r, int g, int b) {
// SDL_SetTextureBlendMode(texture_id,SDL_TEXTUREBLENDMODE_ADD); //hmm
// SDL_SetTextureColorMod(texture_id,r,g,b);
RenderScale(fdst, x,y,alpha,0);
}
void CImg::Render(int x, int y,int alpha,SDL_Rect src) {
// SDL_SetAlpha(image, SDL_SRCALPHA, alpha);
dst.x = x;
dst.y = y;
// SDL_BlitSurface(image, NULL, screen, &dst);
dst.w = src.w;
dst.h = src.h;
SDL_SetTextureAlphaMod(texture_id,(Uint8)alpha);
SDL_RenderCopy(texture_id,&src,&dst);
}
void CImg::RenderScale(SDL_Rect *fdst, int x, int y,int alpha,SDL_Rect *src =0) {
//SDL_SetAlpha(image, SDL_SRCALPHA, alpha);
//SDL_SetTextureAlphaMod(texture_id,(Uint8)alpha);
SDL_RenderCopy(texture_id,src,fdst);
}
void CImg::RenderText() {
}
void CImg::ChangeAlpha(int alpha) {
SDL_SetTextureAlphaMod(texture_id,alpha);
//SDL_SetAlpha(image, SDL_SRCALPHA, alpha);
}