-
Notifications
You must be signed in to change notification settings - Fork 0
/
Basic_Image.cpp
84 lines (78 loc) · 2.35 KB
/
Basic_Image.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
#include "Basic_Image.h"
#include <string>
#include <SDL.h>
#include <SDL_image.h>
#include <iostream>
// Standard Constructor
Basic_Image::Basic_Image(int start_x, int start_y, SDL_Renderer *renderer, SDL_Window *window, std::string file_source) : Widget(start_x, start_y, renderer, window)
{
image_location = file_source;
// Load the image to use
SDL_Surface *image_surface = IMG_Load(image_location.c_str());
//Make sure the load succeeded
if (image_surface == nullptr){
SDL_DestroyRenderer(this->renderer);
SDL_DestroyWindow(this->window);
std::cout << "IMG_Load Error: " << SDL_GetError() << std::endl;
SDL_Quit();
return;
}
// Create texture from loaded image
texture = SDL_CreateTextureFromSurface(renderer, image_surface);
// We don't need the surface after texture creation
SDL_FreeSurface(image_surface);
if (texture == nullptr){
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
std::cout << "SDL_CreateTextureFromSurface Error: " << SDL_GetError() << std::endl;
SDL_Quit();
return;
}
//Set information about texture
SDL_QueryTexture(texture, NULL, NULL, &pos.w, &pos.h);
}
// Draws the image widget
void Basic_Image::draw()
{
if (!hidden){
// Draw test bmp texture
SDL_RenderCopy(renderer, texture, NULL, &pos);
if (!locked){
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderDrawRect(renderer, &pos);
}
}
}
// Changes the image to be drawn
void Basic_Image::changeImage(std::string new_image_location)
{
image_location = new_image_location;
// Load the image to use
SDL_Surface *image_surface = IMG_Load(image_location.c_str());
//Make sure the load succeeded
if (image_surface == nullptr){
SDL_DestroyRenderer(this->renderer);
SDL_DestroyWindow(this->window);
std::cout << "IMG_Load Error: " << SDL_GetError() << std::endl;
SDL_Quit();
return;
}
// Create texture from loaded image
texture = SDL_CreateTextureFromSurface(renderer, image_surface);
// We don't need the surface after texture creation
SDL_FreeSurface(image_surface);
if (texture == nullptr){
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
std::cout << "SDL_CreateTextureFromSurface Error: " << SDL_GetError() << std::endl;
SDL_Quit();
return;
}
//Set information about texture
SDL_QueryTexture(texture, NULL, NULL, &pos.w, &pos.h);
}
// Destructor
Basic_Image::~Basic_Image()
{
SDL_DestroyTexture(texture);
}