-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathImageView.cpp
113 lines (97 loc) · 3.09 KB
/
ImageView.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
//
// ImageView.cpp
// KSDL
//
// Created by Kyle Koser on 5/20/14.
// Copyright (c) 2014 Kyle Koser. All rights reserved.
//
// This class based on CharacterView by Casey Hanley :)
#include "ImageView.h"
ImageView::ImageView( std::string aFileName, SDL_Renderer *aRenderer ) : View({0,0,0,0}, aRenderer), texture(aRenderer) {
static bool sInitialized = false;
if (!sInitialized ) {
//Initialize PNG loading
int imgFlags = IMG_INIT_PNG;
if( !( IMG_Init( imgFlags ) & imgFlags ) ) {
printf( "SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError() );
sInitialized = false;
}
else {
printf("Initialized SDL_image");
sInitialized = true;
}
}
textFile = aFileName;
load();
SDL_Rect imageFrame;
imageFrame.x = 0;
imageFrame.y = 0;
imageFrame.w = texture.getWidth();
imageFrame.h = texture.getHeight();
setFrame(imageFrame);
}
ImageView::ImageView( SDL_Rect aFrame, std::string aFileName, SDL_Renderer *aRenderer) : View(aFrame, aRenderer), texture(aRenderer) {
static bool sInitialized;
if (!sInitialized ) {
//Initialize PNG loading
int imgFlags = IMG_INIT_PNG;
if( !( IMG_Init( imgFlags ) & imgFlags ) ) {
printf( "SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError() );
sInitialized = false;
}
else {
printf("Initialized SDL_image");
sInitialized = true;
}
}
textFile = aFileName;
load();
SDL_Rect imageFrame;
imageFrame.x = aFrame.x;
imageFrame.y = aFrame.y;
imageFrame.w = texture.getWidth();
imageFrame.h = texture.getHeight();
setFrame(imageFrame);
}
//copy constructor
//fixes issue where strange images show up when copied by forcing the texture to reload
ImageView::ImageView(ImageView const &other) : View(other), texture(other.texture) {
textFile = other.textFile;
}
ImageView::~ImageView() {
texture.free();
}
//if the image has not yet been loaded, load it
//lazy loading!
void ImageView::load() {
if (!(texture.getWidth() > 0)) {
texture.loadFromFile(textFile);
}
}
void ImageView::drawInRect(SDL_Rect aRect) {
View::drawInRect(aRect);
load();
// @todo Add support for scaling based on frame size.
texture.render( aRect.x + frame.x, aRect.y + frame.y, &frame, degs, NULL, flipDir);
}
void ImageView::setDegs(double value){
degs=value;
}
//------------------------------------------------------------------------------
double ImageView::getDegs(){
return degs;
}
//------------------------------------------------------------------------------
SDL_RendererFlip ImageView::getDir(){
return flipDir;
}
//------------------------------------------------------------------------------
SDL_RendererFlip ImageView::flipLeft(){
flipDir=SDL_FLIP_NONE;
return flipDir;
}
//------------------------------------------------------------------------------
SDL_RendererFlip ImageView::flipRight(){
flipDir=SDL_FLIP_HORIZONTAL;
return flipDir;
}