Skip to content

Commit

Permalink
Improve texture API a bit. It's now a single function call instead of…
Browse files Browse the repository at this point in the history
… two.
  • Loading branch information
vkoskiv committed Jan 17, 2020
1 parent ab322b9 commit e696050
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 26 deletions.
6 changes: 2 additions & 4 deletions src/datatypes/scene.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,11 @@ int loadScene(struct renderer *r, char *input) {

//Allocate memory for render buffer
//Render buffer is used to store accurate color values for the renderers' internal use
r->state.renderBuffer = newTexture();
allocTextureBuffer(r->state.renderBuffer, float_p, r->prefs.imageWidth, r->prefs.imageHeight, 3);
r->state.renderBuffer = newTexture(float_p, r->prefs.imageWidth, r->prefs.imageHeight, 3);

//Allocate memory for render UI buffer
//This buffer is used for storing UI stuff like currently rendering tile highlights
r->state.uiBuffer = newTexture();
allocTextureBuffer(r->state.uiBuffer, char_p, r->prefs.imageWidth, r->prefs.imageHeight, 4);
r->state.uiBuffer = newTexture(char_p, r->prefs.imageWidth, r->prefs.imageHeight, 4);

//Alloc memory for pthread_create() args
r->state.threadStates = calloc(r->prefs.threadCount, sizeof(struct threadState));
Expand Down
41 changes: 27 additions & 14 deletions src/datatypes/texture.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "texture.h"
#include "color.h"
#include "../utils/logging.h"

//General-purpose blit function
void blit(struct texture *t, struct color c, unsigned x, unsigned y) {
Expand Down Expand Up @@ -73,34 +74,46 @@ struct color textureGetPixelFiltered(struct texture *t, float x, float y) {
return lerp(lerp(topleft, topright, xcopy-xint), lerp(botleft, botright, xcopy-xint), ycopy-yint);
}

struct texture *newTexture() {
struct texture *newTexture(enum precision p, int width, int height, int channels) {
struct texture *t = calloc(1, sizeof(struct texture));
t->width = width;
t->height = height;
t->precision = p;
t->channels = channels;
t->hasAlpha = false;
t->byte_data = NULL;
t->float_data = NULL;
t->colorspace = linear;
t->count = 0;
t->hasAlpha = false;
t->fileType = buffer;
t->offset = 0.0f;
return t;
}

void allocTextureBuffer(struct texture *t, enum precision p, int width, int height, int channels) {
t->width = width;
t->height = height;
t->precision = p;
t->channels = channels;
if (channels > 3) t->hasAlpha = true;
if (channels > 3) {
t->hasAlpha = true;
}

//FIXME: Error checking
switch (t->precision) {
case char_p:
case char_p: {
t->byte_data = calloc(channels * width * height, sizeof(unsigned char));
if (!t->byte_data) {
logr(warning, "Failed to allocate %ix%i texture.\n", width, height);
freeTexture(t);
return NULL;
}
}
break;
case float_p:
case float_p: {
t->float_data = calloc(channels * width * height, sizeof(float));
if (!t->float_data) {
logr(warning, "Failed to allocate %ix%i texture.\n", width, height);
freeTexture(t);
return NULL;
}
}
break;
default:
break;
}
return t;
}

void textureFromSRGB(struct texture *t) {
Expand Down
6 changes: 3 additions & 3 deletions src/datatypes/texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ enum colorspace {

enum precision {
char_p,
float_p
float_p,
none
};

struct renderInfo {
Expand Down Expand Up @@ -49,8 +50,7 @@ struct texture {

struct color;

struct texture *newTexture(void);
void allocTextureBuffer(struct texture *t, enum precision p, int width, int height, int channels);
struct texture *newTexture(enum precision p, int width, int height, int channels);

void blit(struct texture *t, struct color c, unsigned int x, unsigned int y);
struct color textureGetPixel(struct texture *t, unsigned x, unsigned y);
Expand Down
3 changes: 1 addition & 2 deletions src/renderer/renderer.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@
/// @todo Clean this up, it's ugly.
struct texture *renderFrame(struct renderer *r) {

struct texture *output = newTexture();
allocTextureBuffer(output, char_p, r->prefs.imageWidth, r->prefs.imageHeight, 3);
struct texture *output = newTexture(char_p, r->prefs.imageWidth, r->prefs.imageHeight, 3);
output->fileType = r->prefs.imgType;
copyString(r->prefs.imgFileName, &output->fileName);
copyString(r->prefs.imgFilePath, &output->filePath);
Expand Down
5 changes: 2 additions & 3 deletions src/utils/loaders/textureloader.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@

//This is to compensate for the non-standard coordinate system handedness
struct texture *flipHorizontal(struct texture *t) {
struct texture *new = newTexture();
allocTextureBuffer(new, t->precision, t->width, t->height, t->channels);
struct texture *new = newTexture(t->precision, t->width, t->height, t->channels);
new->colorspace = t->colorspace;
new->count = t->count;
if (t->fileName) {
Expand All @@ -44,7 +43,7 @@ struct texture *flipHorizontal(struct texture *t) {
}

struct texture *loadTexture(char *filePath) {
struct texture *new = newTexture();
struct texture *new = newTexture(none, 0, 0, 0);
copyString(filePath, &new->filePath);
//Handle the trailing newline here
//FIXME: This crashes if there is no newline, even though SO said it shouldn't.
Expand Down

0 comments on commit e696050

Please sign in to comment.