Initial api for the image class.

This commit is contained in:
Relintai 2021-03-30 21:19:07 +02:00
parent 3e00e8e760
commit 4eaa7bcfbc
2 changed files with 268 additions and 0 deletions

View File

@ -1,8 +1,63 @@
#ifndef IMAGE_H
#define IMAGE_H
#include "color.h"
#include "rect2.h"
#include "string.h"
#include "vector.h"
#include <SDL.h>
class Image {
public:
void create(Uint32 flags, int width, int height);
void enable_transparent_color(const Color &color);
void disable_transparent_color();
void set_transparent_color(const Color &color);
bool has_transparent_color();
Color get_transparent_color();
Color get_color_mod();
void set_color_mod(const Color &color);
Color get_alpha_mod();
void set_alpha_mod(const Color &color);
SDL_BlendMode get_blend_mode();
void get_blend_mode(const SDL_BlendMode mode);
Rect2 get_clip_rect();
void get_clip_rect(const Rect2 &mode);
Image *duplicate();
void convert(const SDL_PixelFormat *fmt, Uint32 flags);
void convert(Uint32 pixel_format, Uint32 flags);
void fill_rect(const Rect2 &rect, const Color &color);
void fill_rects(const Vector<Rect2> &rects, const Color &color);
void set_pixel(const int x, const int y, const Color &color);
Color get_pixel(const int x, const int y);
void blit_surface(const Image &source, const Rect2 &srcrect, const Rect2 &dstrect);
void lock();
void unlock();
void free();
void load_bmp();
Uint32 get_width() const;
Uint32 get_height() const;
SDL_Surface *get_surface();
Image();
virtual ~Image();
private:
SDL_Surface *_surface;
};
#endif

213
05_sdl_alapok/vector.h Normal file
View File

@ -0,0 +1,213 @@
#ifndef VECTOR_H
#define VECTOR_H
template <class T>
class Vector {
public:
void push_back(const T &element);
void pop_back();
void remove(const int index);
void remove_keep_order(const int index);
void erase(const T &element);
void clear();
bool empty() const;
T get(const int index);
const T &get(const int index) const;
void set(const int index, const T &value);
int size() const;
int capacity() const;
void ensure_capacity(const int capacity);
void resize(const int s);
void append_array(const Vector<T> &other);
int find(const T &val) const;
int *dataw();
const int *data() const;
const T &operator[](const int index) const;
T &operator[](const int index);
Vector();
Vector(int prealloc);
Vector(int prealloc, int grow_by);
private:
T *_data;
int _actual_size;
int _size;
int _grow_by;
};
template <class T>
void Vector<T>::push_back(const T &element) {
ensure_capacity(_size + 1);
_data[_size++] = element;
}
template <class T>
void Vector<T>::pop_back() {
if (_size == 0) {
return;
}
--_size;
}
template <class T>
void Vector<T>::remove(const int index) {
_data[index] = _data[_size - 1];
--_size;
}
template <class T>
void Vector<T>::remove_keep_order(const int index) {
--_size;
for (int i = index; i < _size; ++i) {
_data[i] = _data[i + 1];
}
}
template <class T>
void Vector<T>::erase(const T &element) {
int index = find(element);
if (index != -1) {
remove(index);
}
}
template <class T>
void Vector<T>::clear() {
_size = 0;
}
template <class T>
bool Vector<T>::empty() const {
return _size == 0;
}
template <class T>
T Vector<T>::get(const int index) {
return _data[index];
}
template <class T>
const T &Vector<T>::get(const int index) const {
return _data[index];
}
template <class T>
void Vector<T>::set(const int index, const T &value) {
_data[index] = value;
}
template <class T>
int Vector<T>::size() const {
return _size;
}
template <class T>
int Vector<T>::capacity() const {
return _actual_size;
}
template <class T>
void Vector<T>::ensure_capacity(const int capacity) {
if (capacity <= _actual_size) {
return;
}
int tsize = capacity + _grow_by;
T *nd = new T[tsize];
for (int i = 0; i < _size; ++i) {
nd[i] = _data[i];
}
delete[] _data;
_data = nd;
}
template <class T>
void Vector<T>::resize(const int s) {
ensure_capacity(s);
_size = s;
}
template <class T>
void Vector<T>::append_array(const Vector<T> &other) {
ensure_capacity(_size + other._size);
for (int i = 0; i < other._size; ++i) {
_data[_size++] = other._data[i];
}
}
template <class T>
int Vector<T>::find(const T &val) const {
for (int i = 0; i < _size; ++i) {
if (_data[i] == val) {
return i;
}
}
return -1;
}
template <class T>
int *Vector<T>::dataw() {
return _data;
}
template <class T>
const int *Vector<T>::data() const {
return _data;
}
template <class T>
const T &Vector<T>::operator[](const int index) const {
return _data[index];
}
template <class T>
T &Vector<T>::operator[](const int index) {
return _data[index];
}
template <class T>
Vector<T>::Vector() {
_data = nullptr;
_actual_size = 0;
_size = 0;
_grow_by = 100;
}
template <class T>
Vector<T>::Vector(int prealloc) {
_data = nullptr;
_actual_size = 0;
_size = 0;
_grow_by = 100;
ensure_capacity(prealloc);
}
template <class T>
Vector<T>::Vector(int prealloc, int grow_by) {
_data = nullptr;
_actual_size = 0;
_size = 0;
_grow_by = grow_by;
ensure_capacity(prealloc);
}
#endif