Skip to content

Commit

Permalink
Some code simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
hrydgard committed Dec 13, 2023
1 parent 32f5f08 commit e5d2e09
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 25 deletions.
35 changes: 15 additions & 20 deletions Common/Render/ManagedTexture.cpp
Expand Up @@ -116,7 +116,7 @@ bool TempImage::LoadTextureLevels(const uint8_t *data, size_t size, ImageFileTyp
return numLevels > 0;
}

Draw::Texture *CreateTextureFromTempImage(Draw::DrawContext *draw, const uint8_t *data, size_t dataSize, const TempImage &image, bool generateMips, const char *name) {
Draw::Texture *CreateTextureFromTempImage(Draw::DrawContext *draw, const TempImage &image, bool generateMips, const char *name) {
using namespace Draw;
_assert_(image.levels[0] != nullptr && image.width[0] > 0 && image.height[0] > 0);

Expand Down Expand Up @@ -147,7 +147,7 @@ Draw::Texture *CreateTextureFromFileData(Draw::DrawContext *draw, const uint8_t
if (!image.LoadTextureLevels(data, dataSize, type)) {
return nullptr;
}
Draw::Texture *texture = CreateTextureFromTempImage(draw, data, dataSize, image, generateMips, name);
Draw::Texture *texture = CreateTextureFromTempImage(draw, image, generateMips, name);
image.Free();
return texture;
}
Expand All @@ -165,8 +165,16 @@ Draw::Texture *CreateTextureFromFile(Draw::DrawContext *draw, const char *filena
return texture;
}

bool ManagedTexture::LoadFromFileData(const uint8_t *data, size_t dataSize, ImageFileType type, bool generateMips, const char *name) {
bool ManagedTexture::LoadFromFile(const std::string &filename, ImageFileType type, bool generateMips) {
INFO_LOG(SYSTEM, "ManagedTexture::LoadFromFile (%s)", filename.c_str());
generateMips_ = generateMips;
size_t fileSize;
uint8_t *buffer = g_VFS.ReadFile(filename.c_str(), &fileSize);
if (!buffer) {
filename_.clear();
ERROR_LOG(IO, "Failed to read file '%s'", filename.c_str());
return false;
}

// Free the old texture, if any.
if (texture_) {
Expand All @@ -175,33 +183,20 @@ bool ManagedTexture::LoadFromFileData(const uint8_t *data, size_t dataSize, Imag
}

TempImage image;
if (!image.LoadTextureLevels(data, dataSize, type)) {
if (!image.LoadTextureLevels(buffer, fileSize, type)) {
return false;
}
texture_ = CreateTextureFromTempImage(draw_, data, dataSize, image, generateMips, name);
texture_ = CreateTextureFromTempImage(draw_, image, generateMips, filename.c_str());
image.Free();
return texture_ != nullptr;
}

bool ManagedTexture::LoadFromFile(const std::string &filename, ImageFileType type, bool generateMips) {
INFO_LOG(SYSTEM, "ManagedTexture::LoadFromFile (%s)", filename.c_str());
generateMips_ = generateMips;
size_t fileSize;
uint8_t *buffer = g_VFS.ReadFile(filename.c_str(), &fileSize);
if (!buffer) {
filename_.clear();
ERROR_LOG(IO, "Failed to read file '%s'", filename.c_str());
return false;
}
bool retval = LoadFromFileData(buffer, fileSize, type, generateMips, filename.c_str());
if (retval) {
if (texture_) {
filename_ = filename;
} else {
filename_.clear();
ERROR_LOG(IO, "Failed to load texture '%s'", filename.c_str());
}
delete[] buffer;
return retval;
return texture_ != 0;
}

std::unique_ptr<ManagedTexture> CreateManagedTextureFromFile(Draw::DrawContext *draw, const char *filename, ImageFileType type, bool generateMips) {
Expand Down
5 changes: 1 addition & 4 deletions Common/Render/ManagedTexture.h
Expand Up @@ -16,15 +16,12 @@ enum ImageFileType {

class ManagedTexture {
public:
ManagedTexture(Draw::DrawContext *draw) : draw_(draw) {
}
ManagedTexture(Draw::DrawContext *draw) : draw_(draw) {}
~ManagedTexture() {
if (texture_)
texture_->Release();
}

bool LoadFromFile(const std::string &filename, ImageFileType type = ImageFileType::DETECT, bool generateMips = false);
bool LoadFromFileData(const uint8_t *data, size_t dataSize, ImageFileType type, bool generateMips, const char *name);
Draw::Texture *GetTexture(); // For immediate use, don't store.
int Width() const { return texture_->Width(); }
int Height() const { return texture_->Height(); }
Expand Down
1 change: 1 addition & 0 deletions Common/UI/AsyncImageFileView.cpp
Expand Up @@ -2,6 +2,7 @@
#include "Common/UI/AsyncImageFileView.h"
#include "Common/UI/Context.h"
#include "Common/Render/DrawBuffer.h"
#include "Common/Render/ManagedTexture.h"

AsyncImageFileView::AsyncImageFileView(const Path &filename, UI::ImageSizeMode sizeMode, UI::LayoutParams *layoutParams)
: UI::Clickable(layoutParams), canFocus_(true), filename_(filename), color_(0xFFFFFFFF), sizeMode_(sizeMode), textureFailed_(false), fixedSizeW_(0.0f), fixedSizeH_(0.0f) {}
Expand Down
2 changes: 1 addition & 1 deletion Common/UI/AsyncImageFileView.h
@@ -1,10 +1,10 @@
#pragma once

#include "Common/UI/View.h"
#include "Common/Render/ManagedTexture.h"
#include "Common/File/Path.h"

class UIContext;
class ManagedTexture;

// AsyncImageFileView loads a texture from a file, and reloads it as necessary.
// TODO: Actually make async, doh.
Expand Down

0 comments on commit e5d2e09

Please sign in to comment.