Newer
Older
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
#include <cassert>
#include <time.h>
#include <stdlib.h>
#include <iostream>
#include <filesystem>
#include<fstream>
#include "ImageViewer.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include <iostream>
using namespace std;
ImageViewer::ImageViewer()
{
m_surface = nullptr;
m_texture = nullptr;
m_hasChanged = false;
}
ImageViewer::~ImageViewer()
{
SDL_FreeSurface(m_surface);
SDL_DestroyTexture(m_texture);
m_surface = nullptr;
m_texture = nullptr;
m_hasChanged = false;
}
void ImageViewer::loadFromFile(const char *filename, SDL_Renderer *m_renderer)
{
m_surface = IMG_Load(filename);
if (m_surface == nullptr)
{
string nfn = string(filename);
int n = 0;
while ((m_surface == nullptr) && (n < 3))
{
nfn = string("../") + nfn;
m_surface = IMG_Load(nfn.c_str());
}
}
if (m_surface == nullptr)
{
std::filesystem::path currentPath = std::filesystem::current_path();
std::cout << "Répertoire courant : " << currentPath << std::endl;
cout << "Error: cannot load " << filename << endl;
exit(1);
}
SDL_Surface *surfaceCorrectPixelFormat = SDL_ConvertSurfaceFormat(m_surface, SDL_PIXELFORMAT_ARGB8888, 0);
SDL_FreeSurface(m_surface);
m_surface = surfaceCorrectPixelFormat;
m_texture = SDL_CreateTextureFromSurface(m_renderer, surfaceCorrectPixelFormat);
if (m_texture == nullptr)
{
cout << "Error: problem to create the texture of " << filename << endl;
exit(1);
}
}
void ImageViewer::loadFromCurrentSurface(SDL_Renderer *m_renderer)
{
m_texture = SDL_CreateTextureFromSurface(m_renderer, m_surface);
if (m_texture == nullptr)
{
cout << "Error: problem to create the texture from surface " << endl;
exit(1);
}
}
void ImageViewer::draw(SDL_Renderer *m_renderer, int x, int y, int w, int h)
{
int ok;
SDL_Rect r;
r.x = x;
r.y = y;
r.w = (w < 0) ? m_surface->w : w;
r.h = (h < 0) ? m_surface->h : h;
if (m_hasChanged)
{
ok = SDL_UpdateTexture(m_texture, nullptr, m_surface->pixels, m_surface->pitch);
assert(ok == 0);
m_hasChanged = false;
}
ok = SDL_RenderCopy(m_renderer, m_texture, nullptr, &r);
assert(ok == 0);
}
SDL_Texture *ImageViewer::getTexture() const { return m_texture; }
void ImageViewer::setSurface(SDL_Surface *surf) { m_surface = surf; }
void ImageViewer::afficher(const Image & im)
{
}