Warning fixes, refactor

This commit is contained in:
MihailRis 2023-09-19 23:25:17 +03:00 committed by GitHub
parent eaf1fa54cd
commit 4f20abe7ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 67 additions and 55 deletions

View File

@ -171,7 +171,7 @@ bool WorldFiles::readChunk(int x, int y, char* out){
input.read(mainBufferIn, compressedSize);
input.close();
decompressRLE(mainBufferIn, compressedSize, out, CHUNK_VOL);
decompressRLE((unsigned char*)mainBufferIn, compressedSize, (unsigned char*)out, CHUNK_VOL);
return true;
}
@ -274,7 +274,7 @@ unsigned int WorldFiles::writeRegion(char* out, int x, int y, char** region){
} else {
int2Bytes(offset, out, i*4);
unsigned int compressedSize = compressRLE(chunk, CHUNK_VOL, compressed);
unsigned int compressedSize = compressRLE((unsigned char*)chunk, CHUNK_VOL, (unsigned char*)compressed);
int2Bytes(compressedSize, out, offset);
offset += 4;

View File

@ -2,6 +2,8 @@
#include <fstream>
#include <iostream>
#include <memory>
#include <stdint.h>
bool write_binary_file_part(std::string filename, const char* data, size_t offset, size_t size){
std::ofstream output(filename, std::ios::out | std::ios::binary | std::ios::in);
@ -48,18 +50,18 @@ char* read_binary_file(std::string filename, size_t& length) {
length = input.tellg();
input.seekg(0, std::ios_base::beg);
char* data = new char[length];
input.read(data, length);
std::unique_ptr<char> data {new char[length]};
input.read(data.get(), length);
input.close();
return data;
return data.release();
}
// returns decompressed length
unsigned int decompressRLE(const char* src, unsigned int length, char* dst, unsigned int targetLength){
unsigned int offset = 0;
for (unsigned int i = 0; i < length;){
size_t decompressRLE(const ubyte* src, size_t length, ubyte* dst, size_t targetLength){
size_t offset = 0;
for (size_t i = 0; i < length;){
unsigned char counter = src[i++];
char c = src[i++];
unsigned char c = src[i++];
for (unsigned int j = 0; j <= counter; j++){
dst[offset++] = c;
}
@ -67,12 +69,12 @@ unsigned int decompressRLE(const char* src, unsigned int length, char* dst, unsi
return offset;
}
unsigned int calcRLE(const char* src, unsigned int length) {
unsigned int offset = 0;
unsigned int counter = 0;
char c = src[0];
for (unsigned int i = 0; i < length; i++){
char cnext = src[i];
size_t calcRLE(const ubyte* src, size_t length) {
size_t offset = 0;
size_t counter = 0;
ubyte c = src[0];
for (size_t i = 0; i < length; i++){
ubyte cnext = src[i];
if (cnext != c || counter == 255){
offset += 2;
c = cnext;
@ -85,12 +87,14 @@ unsigned int calcRLE(const char* src, unsigned int length) {
}
// max result size = length * 2; returns compressed length
unsigned int compressRLE(const char* src, unsigned int length, char* dst) {
unsigned int offset = 0;
unsigned int counter = 0;
char c = src[0];
for (unsigned int i = 1; i < length; i++){
char cnext = src[i];
size_t compressRLE(const ubyte* src, size_t length, ubyte* dst) {
if (length == 0)
return 0;
size_t offset = 0;
uint counter = 0;
ubyte c = src[0];
for (size_t i = 1; i < length; i++){
ubyte cnext = src[i];
if (cnext != c || counter == 255){
dst[offset++] = counter;
dst[offset++] = c;

View File

@ -2,6 +2,7 @@
#define FILES_FILES_H_
#include <string>
#include "../typedefs.h"
extern bool write_binary_file(std::string filename, const char* data, size_t size);
extern unsigned int append_binary_file(std::string filename, const char* data, size_t size);
@ -9,8 +10,8 @@ extern bool read_binary_file(std::string filename, char* data, size_t size);
extern bool read_binary_file(std::string filename, char* data, size_t offset, size_t size);
extern char* read_binary_file(std::string filename, size_t& length);
extern unsigned int calcRLE(const char* src, unsigned int length);
extern unsigned int compressRLE(const char* src, unsigned int length, char* dst);
extern unsigned int decompressRLE(const char* src, unsigned int length, char* dst, unsigned int targetLength);
extern size_t calcRLE(const ubyte* src, size_t length);
extern size_t compressRLE(const ubyte* src, size_t length, ubyte* dst);
extern size_t decompressRLE(const ubyte* src, size_t length, ubyte* dst, size_t targetLength);
#endif /* FILES_FILES_H_ */

View File

@ -415,7 +415,6 @@ inline void _renderBlock(std::vector<float>& buffer, int x, int y, int z, const
}
inline void _renderBlockShadeless(std::vector<float>& buffer, int x, int y, int z, const Chunk** chunks, voxel vox, size_t& index){
float l;
float uvsize = 1.0f/16.0f;
Block* block = Block::blocks[vox.id];
@ -495,7 +494,7 @@ inline void _renderBlockShadeless(std::vector<float>& buffer, int x, int y, int
inline void _renderXBlock(std::vector<float>& buffer, int x, int y, int z, const Chunk** chunks, voxel vox, size_t& index){
Block* block = Block::blocks[vox.id];
int rand = ((x * z + y) xor (z * y - x)) * (z + y);
int rand = ((x * z + y) ^ (z * y - x)) * (z + y);
float xs = (float)(char)rand / 512;
float zs = (float)(char)(rand >> 8) / 512;

View File

@ -2,6 +2,8 @@
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include "typedefs.h"
#include "Assets.h"
#include "graphics/Shader.h"
#include "graphics/Batch2D.h"
@ -125,12 +127,12 @@ void HudRenderer::draw(Level* level, Assets* assets){
}
if (!Events::_cursor_locked) { //inventory
int size = 48;
int step = 64;
int inv_wm = step*10;
int inv_hm = step*8;
int inv_w = inv_wm - (step - size);
int inv_h = inv_hm - (step - size);
uint size = 48;
uint step = 64;
uint inv_wm = step*10;
uint inv_hm = step*8;
uint inv_w = inv_wm - (step - size);
uint inv_h = inv_hm - (step - size);
int inv_x = (Window::width - (inv_w)) / 2;
int inv_y = (Window::height - (inv_h)) / 2;
int xs = (Window::width - inv_w + step)/2;
@ -146,7 +148,7 @@ void HudRenderer::draw(Level* level, Assets* assets){
vec4 tint = vec4(1.0f);
int mx = Events::x;
int my = Events::y;
int count = (inv_w / step) * (inv_h / step) + 1;
uint count = (inv_w / step) * (inv_h / step) + 1;
//back
batch->texture(nullptr);
@ -162,7 +164,7 @@ void HudRenderer::draw(Level* level, Assets* assets){
0.75f, 0.75f, 0.75f, 0.75f, 0.75f, 0.75f, 4);
batch->color = vec4(0.35f, 0.35f, 0.35f, 1.0f);
for (unsigned i = 1; i < count; i++) {
for (uint i = 1; i < count; i++) {
x = xs + step * ((i-1) % (inv_w / step));
y = ys + step * ((i-1) / (inv_w / step));
// batch->rect(x-2, y-2, size+4, size+4);
@ -185,13 +187,13 @@ void HudRenderer::draw(Level* level, Assets* assets){
//front
batch->texture(blocks);
for (unsigned i = 1; i < count; i++) {
for (uint i = 1; i < count; i++) {
Block* cblock = Block::blocks[i];
if (cblock == nullptr)
break;
x = xs + step * ((i-1) % (inv_w / step));
y = ys + step * ((i-1) / (inv_w / step));
if (mx > x && mx < x + size && my > y && my < y + size) {
if (mx > x && mx < x + (int)size && my > y && my < y + (int)size) {
tint.r *= 1.2f;
tint.g *= 1.2f;
tint.b *= 1.2f;

View File

@ -188,10 +188,10 @@ void PlayerController::update_interaction(){
int x = (int)iend.x;
int y = (int)iend.y;
int z = (int)iend.z;
uint8_t states;
uint8_t states = 0;
if (Block::blocks[player->choosenBlock]->rotatable){
states = states & 0b11111100;
// states = states & 0b11111100;
// if (abs(norm.x) > abs(norm.z)){
// if (abs(norm.x) > abs(norm.y)) states = states | 0b00000001;
// if (abs(norm.x) < abs(norm.y)) states = states | 0b00000010;

4
src/typedefs.h Normal file
View File

@ -0,0 +1,4 @@
#include <stdint.h>
typedef unsigned int uint;
typedef unsigned char ubyte;

View File

@ -75,7 +75,7 @@ void write_world(World* world, Level* level){
world->wfile->writePlayer(level->player);
}
void update_level(World* world, Level* level, float delta, long frame, VoxelRenderer* renderer) {
void update_level(World* world, Level* level, float delta) {
level->playerController->update_controls(delta);
if (Events::_cursor_locked){
level->playerController->update_interaction();
@ -151,10 +151,10 @@ void mainloop(Level* level, Assets* assets) {
}
}
update_level(world, level, delta, frame, worldRenderer.renderer);
update_level(world, level, delta);
int freeLoaders = level->chunksController->countFreeLoaders();
for (int i = 0; i < freeLoaders; i++)
level->chunksController->_buildMeshes(worldRenderer.renderer, frame);
level->chunksController->_buildMeshes();
freeLoaders = level->chunksController->countFreeLoaders();
for (int i = 0; i < freeLoaders; i++)
level->chunksController->calculateLights();

View File

@ -185,7 +185,7 @@ void ChunksController::calculateLights() {
freeLoader->lights(chunk, (Chunk**)closes);
}
bool ChunksController::_buildMeshes(VoxelRenderer* renderer, int tick) {
bool ChunksController::_buildMeshes() {
const int w = chunks->w;
const int d = chunks->d;

View File

@ -22,7 +22,7 @@ public:
int countFreeLoaders();
bool loadVisible(WorldFiles* worldFiles);
void calculateLights();
bool _buildMeshes(VoxelRenderer* renderer, int tick);
bool _buildMeshes();
};
#endif /* VOXELS_CHUNKSCONTROLLER_H_ */

View File

@ -31,11 +31,11 @@ public:
}
void setSeed(int number){
seed = ((unsigned short)(number*23729) xor (unsigned short)(number+16786));
seed = ((unsigned short)(number*23729) ^ (unsigned short)(number+16786));
rand();
}
void setSeed(int number1,int number2){
seed = (((unsigned short)(number1*23729) or (unsigned short)(number2%16786)) xor (unsigned short)(number2*number1));
seed = (((unsigned short)(number1*23729) | (unsigned short)(number2%16786)) ^ (unsigned short)(number2*number1));
rand();
}
};

View File

@ -15,7 +15,7 @@ bool Events::_cursor_started = false;
#define _MOUSE_BUTTONS 1024
void cursor_position_callback(GLFWwindow* window, double xpos, double ypos){
void cursor_position_callback(GLFWwindow*, double xpos, double ypos){
if (Events::_cursor_started){
Events::deltaX += xpos-Events::x;
Events::deltaY += ypos-Events::y;
@ -27,7 +27,7 @@ void cursor_position_callback(GLFWwindow* window, double xpos, double ypos){
Events::y = ypos;
}
void mouse_button_callback(GLFWwindow* window, int button, int action, int mode){
void mouse_button_callback(GLFWwindow*, int button, int action, int){
if (action == GLFW_PRESS){
Events::_keys[_MOUSE_BUTTONS+button] = true;
Events::_frames[_MOUSE_BUTTONS+button] = Events::_current;
@ -38,7 +38,7 @@ void mouse_button_callback(GLFWwindow* window, int button, int action, int mode)
}
}
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode) {
void key_callback(GLFWwindow*, int key, int /*scancode*/, int action, int /*mode*/) {
if (action == GLFW_PRESS){
Events::_keys[key] = true;
Events::_frames[key] = Events::_current;
@ -49,7 +49,7 @@ void key_callback(GLFWwindow* window, int key, int scancode, int action, int mod
}
}
void window_size_callback(GLFWwindow* window, int width, int height){
void window_size_callback(GLFWwindow*, int width, int height){
glViewport(0,0, width, height);
Window::width = width;
Window::height = height;

View File

@ -4,10 +4,10 @@
#include "Window.h"
GLFWwindow* Window::window;
int Window::width = 0;
int Window::height = 0;
uint Window::width = 0;
uint Window::height = 0;
int Window::initialize(int width, int height, const char* title){
int Window::initialize(uint width, uint height, const char* title){
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);

View File

@ -1,14 +1,16 @@
#ifndef WINDOW_WINDOW_H_
#define WINDOW_WINDOW_H_
#include "../typedefs.h"
class GLFWwindow;
class Window {
public:
static int width;
static int height;
static uint width;
static uint height;
static GLFWwindow* window; // не лучшее решение делать window публичным
static int initialize(int width, int height, const char* title);
static int initialize(uint width, uint height, const char* title);
static void terminate();
static void viewport(int x, int y, int width, int height);