-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCImage.cpp
88 lines (63 loc) · 1.62 KB
/
CImage.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
/*
* This file is distributed under the MIT License.
* See LICENSE file for details.
*/
#include "CConsole.h"
SDL_Texture * CImage::SurfaceToTexture(SDL_Surface *srf){
SDL_Texture *text=SDL_CreateTextureFromSurface( CConsole::getInstance()->getRenderer(), srf );
if(!text){
fprintf(stderr,"Error converting surface %s\n",SDL_GetError());
}
return text;
}
SDL_Surface * CImage::createSurface(int width, int height){
SDL_Surface * surface = SDL_CreateRGBSurface(0, width, height, 32,
RMASK32, GMASK32, BMASK32, AMASK32);
if (surface == NULL) {
fprintf(stderr,"SDL_CreateRGBSurface() failed: %s", SDL_GetError());
}
return surface;
}
CImage::CImage(){
mWidth= mHeight=0;
texture=NULL;
// default pixmap..
}
// create image from native ...
bool CImage::load(const char *file){
SDL_Surface *srf=SDL_LoadBMP(file);
if(srf!=NULL){
if((texture=CImage::SurfaceToTexture(srf)) !=NULL){
this->mWidth=srf->w;
this->mHeight=srf->h;
printf("Loaded image %s (%ix%i).\n",file,this->mWidth,this->mHeight);
return true;
}
else{
fprintf(stderr,"SurfaceToTexture:%s\n",SDL_GetError());
}
}
else{
fprintf(stderr,"SDL_LoadBMP:%s\n",SDL_GetError());
}
return false;
}
SDL_Texture * CImage::getTexture(){
return texture;
}
int CImage::getWidth(){
return mWidth;
}
int CImage::getHeight(){
return mHeight;
}
void CImage::destroy(){
if(texture != NULL){
printf("Image destroyed!\n");
SDL_DestroyTexture(texture);
}
texture=NULL;
}
CImage::~CImage(){
destroy();
}