-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8cf66c7
commit d38ce7d
Showing
45 changed files
with
23,003 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
cmake_minimum_required(VERSION 3.5) | ||
project(main) | ||
|
||
set(CMAKE_CXX_STANDARD 14) | ||
|
||
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/bin) | ||
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/bin) | ||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/bin) | ||
|
||
set(SOURCE_FILES | ||
glad.c | ||
Image.cpp | ||
Player.cpp | ||
main.cpp) | ||
|
||
set(ADDITIONAL_INCLUDE_DIRS | ||
dependencies/include/GLAD) | ||
set(ADDITIONAL_LIBRARY_DIRS | ||
dependencies/lib) | ||
set(ADDITIONAL_RUNTIME_LIBRARY_DIRS | ||
dependencies/bin) | ||
|
||
set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}") | ||
|
||
if(WIN32) | ||
set(ADDITIONAL_INCLUDE_DIRS | ||
${ADDITIONAL_INCLUDE_DIRS} | ||
dependencies/include) | ||
link_directories(${ADDITIONAL_LIBRARY_DIRS}) | ||
else() | ||
find_package(glfw3 REQUIRED) | ||
endif() | ||
|
||
include_directories(${ADDITIONAL_INCLUDE_DIRS}) | ||
|
||
find_package(OpenGL REQUIRED) | ||
|
||
add_executable(main ${SOURCE_FILES}) | ||
|
||
target_include_directories(main PRIVATE ${OPENGL_INCLUDE_DIR}) | ||
|
||
if(WIN32) | ||
add_custom_command(TARGET main POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_directory "${PROJECT_SOURCE_DIR}/dependencies/bin" $<TARGET_FILE_DIR:main>) | ||
set_target_properties(main PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}") | ||
target_compile_options(main PRIVATE) | ||
target_link_libraries(main LINK_PUBLIC ${OPENGL_gl_LIBRARY} glfw3dll) | ||
else() | ||
target_compile_options(main PRIVATE -Wnarrowing) | ||
target_link_libraries(main LINK_PUBLIC ${OPENGL_gl_LIBRARY} glfw rt dl) | ||
endif() | ||
|
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#ifndef MAIN_GENERAL_H | ||
#define MAIN_GENERAL_H | ||
struct Point | ||
{ | ||
int x; | ||
int y; | ||
}; | ||
|
||
struct GameTime | ||
{ | ||
//explicit GameTime(double time) : cur_sec(time) {}; | ||
//static GameTime NowTime; | ||
|
||
static GameTime* Now() { static GameTime ret {}; return &ret; } | ||
|
||
inline void SetCur(double sec) { cur_sec = sec; } | ||
inline double GetSecAfter(double time_sec) { return cur_sec - time_sec; } | ||
inline double GetTime() { return cur_sec; } | ||
|
||
private: | ||
double cur_sec = 0; | ||
}; | ||
|
||
enum class E_Dir | ||
{ | ||
UP, | ||
DOWN, | ||
LEFT, | ||
RIGHT | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
#include "Image.h" | ||
|
||
#define STB_IMAGE_IMPLEMENTATION | ||
#include "stb_image.h" | ||
#define STB_IMAGE_WRITE_IMPLEMENTATION | ||
#include "stb_image_write.h" | ||
|
||
#include <iostream> | ||
|
||
#include "General.h" | ||
#include <algorithm> | ||
#include <map> | ||
|
||
Image::Image(const std::string &a_path) | ||
{ | ||
if((data = (Pixel*)stbi_load(a_path.c_str(), &width, &height, &channels, 4)) != nullptr) | ||
{ | ||
size = width * height * channels; | ||
} | ||
} | ||
|
||
Image::Image(const std::string &a_path, int scale_x, int scale_y) | ||
{ | ||
auto temp = Image(a_path); | ||
ImgClone(temp, scale_x, scale_y); | ||
} | ||
|
||
Image::Image(const std::string &a_path, int scale) : Image(a_path, scale, scale) {} | ||
|
||
Image::Image(int a_width, int a_height, int a_channels) | ||
{ | ||
data = new Pixel[a_width * (size_t)a_height]; | ||
|
||
if(data != nullptr) | ||
{ | ||
width = a_width; | ||
height = a_height; | ||
size = a_width * a_height * a_channels; | ||
channels = a_channels; | ||
self_allocated = true; | ||
} | ||
} | ||
|
||
Image::Image(const Image ©) { ImgClone(copy, 1, 1); } | ||
|
||
Image::Image(const Image ©, int scale) : Image(copy, scale, scale) {} | ||
|
||
void Image::ImgClone(const Image ©, int scale_x, int scale_y) | ||
{ | ||
if (scale_x <= 0 || scale_y <= 0)std::abort(); | ||
|
||
width = copy.width * scale_x; | ||
height = copy.height * scale_y; | ||
size = copy.size * scale_x * scale_y; | ||
channels = copy.channels; | ||
|
||
if (scale_x == 1 && scale_y == 1) { | ||
data = new Pixel[width * (size_t)height]; | ||
//for (int z = 0; z < width * height; z++)data[z] = copy.data[z]; | ||
std::copy(copy.data, copy.data + (width * (size_t)height), data); | ||
self_allocated = true; | ||
return; | ||
} | ||
|
||
data = new Pixel[width * (size_t)height]; | ||
|
||
int c_x = 0; | ||
int c_y = 0; | ||
for (int y = 0; y < height; y++, c_y += (y % scale_y == 0), c_x = 0) | ||
for (int x = 0; x < width; x++, c_x += (x % scale_x == 0)) | ||
SetPixel(x, y, copy.GetPixel(c_x, c_y)); | ||
|
||
self_allocated = true; | ||
} | ||
|
||
Image::Image(const Image ©, int scale_x, int scale_y) { ImgClone(copy, scale_x, scale_y); } | ||
|
||
Image::Image(Image&& a) noexcept : width(a.width), height(a.height), size(a.size), channels(a.channels), data(a.data), self_allocated(a.self_allocated) | ||
{ | ||
a.data = nullptr; | ||
} | ||
|
||
|
||
int Image::Save(const std::string &a_path) | ||
{ | ||
auto extPos = a_path.find_last_of('.'); | ||
if(a_path.substr(extPos, std::string::npos) == ".png" || a_path.substr(extPos, std::string::npos) == ".PNG") | ||
{ | ||
stbi_write_png(a_path.c_str(), width, height, channels, data, width * channels); | ||
} | ||
else if(a_path.substr(extPos, std::string::npos) == ".jpg" || a_path.substr(extPos, std::string::npos) == ".JPG" || | ||
a_path.substr(extPos, std::string::npos) == ".jpeg" || a_path.substr(extPos, std::string::npos) == ".JPEG") | ||
{ | ||
stbi_write_jpg(a_path.c_str(), width, height, channels, data, 100); | ||
} | ||
else | ||
{ | ||
std::cerr << "Unknown file extension: " << a_path.substr(extPos, std::string::npos) << "in file name" << a_path << "\n"; | ||
return 1; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
void Image::Draw(Image &canvas, Point p, bool flip_OX, bool flip_OY) const | ||
{ | ||
int c_w; | ||
int c_h; | ||
if (p.x <= -width || | ||
p.y <= -height || | ||
p.x > (c_w = canvas.Width()) || | ||
p.y > (c_h = canvas.Height()) ) return; | ||
|
||
int start_x = std::max(p.x, 0); | ||
int start_y = std::max(p.y, 0); | ||
|
||
int end_x = std::min(p.x + width, c_w); | ||
int end_y = std::min(p.y + height, c_h); | ||
|
||
int this_x_zero = (p.x < 0) ? -p.x : 0; | ||
int this_x = this_x_zero; | ||
int this_y = (p.y < 0) ? -p.y : 0; | ||
|
||
//for(int y = start_y; y < end_y; y++, this_x = this_x_zero, this_y++) | ||
//for(int x = start_x; x < end_x; x++, this_x++) | ||
// canvas.MixPixel(x, y, GetPixel(this_x, this_y)); | ||
|
||
#define Z(th_x, th_y) \ | ||
for(int y = start_y; y < end_y; y++, this_x = this_x_zero, this_y++) \ | ||
for (int x = start_x; x < end_x; x++, this_x++) \ | ||
canvas.MixPixel(x, y, GetPixel(th_x, th_y)) | ||
|
||
if (!flip_OX && !flip_OY)Z(this_x, this_y); | ||
else if (flip_OX && !flip_OY)Z(this_x, height - 1 - this_y); | ||
else if (!flip_OX && flip_OY)Z(width - 1 - this_x, this_y); | ||
else /*if (flip_OY && flip_OX)*/Z(width - 1 - this_x, height - 1 - this_y); | ||
|
||
#undef Z | ||
} | ||
|
||
void Image::PixelsChange(std::function<Pixel(Pixel)> PixFunc, bool with_hash_pixel) | ||
{ | ||
if (!with_hash_pixel) { | ||
for (int y = 0; y < height; y++) | ||
for (int x = 0; x < width; x++) | ||
SetPixel(x, y, PixFunc(GetPixel(x, y))); | ||
return; | ||
} | ||
|
||
std::map<Pixel, Pixel> already {}; | ||
for (int y = 0; y < height; y++) | ||
for (int x = 0; x < width; x++) { | ||
auto pix0 = GetPixel(x, y); | ||
auto p = already.find(pix0); | ||
if (p != already.end()) SetPixel(x, y, p->second); | ||
else { | ||
auto pix1 = PixFunc(pix0); | ||
already.insert({pix0, pix1}); | ||
SetPixel(x, y, pix1); | ||
} | ||
} | ||
} | ||
|
||
Image::~Image() | ||
{ | ||
if (data == nullptr)return; | ||
|
||
if (self_allocated) | ||
delete[] data; | ||
else { | ||
stbi_image_free(data); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
#ifndef MAIN_IMAGE_H | ||
#define MAIN_IMAGE_H | ||
|
||
#include <string> | ||
#include <functional> | ||
#include "General.h" | ||
|
||
constexpr int tileSize = 16; | ||
|
||
#pragma region Pixel | ||
struct Pixel | ||
{ | ||
uint8_t r; | ||
uint8_t g; | ||
uint8_t b; | ||
uint8_t a; | ||
|
||
Pixel &operator+=(const Pixel &add) | ||
{ | ||
if (add.a == 255) { r = add.r; g = add.g; b = add.b; return *this; } | ||
if (add.a == 0)return *this; | ||
double k = (add.a / 255.0); | ||
r = (uint8_t)((add.r - (int)r) * k + r); | ||
g = (uint8_t)((add.g - (int)g) * k + g); | ||
b = (uint8_t)((add.b - (int)b) * k + b); | ||
return *this; | ||
} | ||
|
||
inline bool operator< (Pixel const &other) | ||
{ | ||
if (r != other.r) return r < other.r; | ||
if (g != other.g) return g < other.g; | ||
if (b != other.b) return b < other.b; | ||
return a < other.a; | ||
} | ||
}; | ||
|
||
inline bool operator< (const Pixel a, const Pixel b) | ||
{ | ||
if (a.r != b.r) return a.r < b.r; | ||
if (a.g != b.g) return a.g < b.g; | ||
if (a.b != b.b) return a.b < b.b; | ||
return a.a < b.a; | ||
} | ||
#pragma endregion | ||
|
||
|
||
constexpr Pixel backgroundColor{0, 0, 0, 0}; | ||
|
||
struct Image | ||
{ | ||
explicit Image(const std::string &a_path); | ||
Image(const std::string &a_path, int scale); | ||
Image(const std::string &a_path, int scale_x, int scale_y); | ||
|
||
Image(int a_width, int a_height, int a_channels); | ||
|
||
Image(const Image ©, int scale); | ||
Image(const Image ©, int scale_x, int scale_y); | ||
|
||
Image(const Image ©); | ||
Image(Image && a) noexcept; | ||
|
||
Image &operator=(const Image &other) | ||
{ | ||
std::abort(); | ||
return *this; | ||
} | ||
Image &operator=(Image &&other) noexcept | ||
{ | ||
if (&other == this)return *this; | ||
width = other.width; | ||
height = other.height; | ||
size = other.size; | ||
channels = other.channels; | ||
self_allocated = other.self_allocated; | ||
|
||
data = other.data; | ||
other.data = nullptr; | ||
|
||
return *this; | ||
} | ||
|
||
int Save(const std::string &a_path); | ||
|
||
inline int Width() const { return width; } | ||
inline int Height() const { return height; } | ||
inline int Channels() const { return channels; } | ||
inline size_t Size() const { return size; } | ||
inline Pixel* Data() { return data; } | ||
|
||
inline Pixel GetPixel(int x, int y) const { return data[width * y + x]; } | ||
|
||
inline void MixPixel(int x, int y, const Pixel &pix) { data[width * y + x] += pix; } | ||
|
||
inline void SetPixel(int x, int y, const Pixel &pix) { data[width * y + x] = pix; } | ||
|
||
void Draw(Image &canvas, Point p, bool flip_OX = false, bool flip_OY = false) const; | ||
|
||
/// <summary>chnage image : image[x,y] = PixFunc(image[x,y])</summary> | ||
/// <param name="with_save_pixel">if we know that dif color on that image few then we can hash PixFunc result value</param> | ||
void PixelsChange(std::function<Pixel(Pixel)> PixFunc, bool with_hash_pixel); | ||
|
||
~Image(); | ||
|
||
private: | ||
void ImgClone(const Image ©, int scale_x, int scale_y); // for write wo copypast and wo temp obj | ||
|
||
int width = -1; | ||
int height = -1; | ||
int channels = 3; | ||
size_t size = 0; | ||
Pixel *data = nullptr; | ||
bool self_allocated = false; | ||
}; | ||
|
||
|
||
|
||
#endif //MAIN_IMAGE_H |
Empty file.
Oops, something went wrong.