Skip to content

Commit

Permalink
fix more graphics issues (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
kazzmir authored Jan 20, 2024
1 parent 2365a31 commit cc2a419
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 6 deletions.
3 changes: 3 additions & 0 deletions src/main-menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ static const int DEFAULT_DEBUG = 0;
#endif

#include "r-tech1/graphics/bitmap.h"
#include "r-tech1/graphics/texture-cache.h"

#include "factory/collector.h"
#include "r-tech1/network/network.h"
Expand Down Expand Up @@ -1010,6 +1011,8 @@ int rtech_main(int argc, char ** argv){

Util::Parameter<Util::ReferenceCount<Menu::FontInfo> > defaultMenuFont(Menu::menuFontParameter, Util::ReferenceCount<Menu::FontInfo>(new Menu::RelativeFontInfo(*defaultFont.current(), Configuration::getMenuFontWidth(), Configuration::getMenuFontHeight())));

Graphics::LocalTextureCache defaultTextureCache;

startMain(actions, allow_quit);

Configuration::saveConfiguration();
Expand Down
3 changes: 2 additions & 1 deletion src/r-tech1/events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,8 @@ static void doStandardLoop(Logic & logic, Draw & draw){
} else {
draw.updateFrames();
screen.clear();
draw.draw(screen.aspectRatio(640, 480));
// draw.draw(screen.aspectRatio(640, 480));
draw.draw(screen);
if (useTouchOverlay()){
InputManager::getTouch()->drawTouchOverlay(screen);
}
Expand Down
2 changes: 2 additions & 0 deletions src/r-tech1/graphics/bitmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ int getBlue(const Color & x);
int getGreen(const Color & x);
int getAlpha(const Color & x);

Bitmap makeRoundedRect(int width, int height, int radius, const Color & fillColor, const Color & borderColor);

Color MaskColor();

enum QualityFilter{
Expand Down
80 changes: 77 additions & 3 deletions src/r-tech1/graphics/sdl2/bitmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,21 @@ bool operator<(const INTERNAL_COLOR&, const INTERNAL_COLOR&){
return false;
}

Graphics::Bitmap Graphics::makeRoundedRect(int width, int height, int radius, const Graphics::Color & fillColor, const Graphics::Color & borderColor){

SDL_Texture* texture = SDL_CreateTexture(global_handler->renderer, SDL_PIXELFORMAT_RGBA32, SDL_TEXTUREACCESS_TARGET, width, height);
SDL_SetRenderTarget(global_handler->renderer, texture);
SDL_SetRenderDrawBlendMode(global_handler->renderer, SDL_BLENDMODE_NONE);
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
SDL_SetRenderDrawColor(global_handler->renderer, 0, 0, 0, 0);
SDL_RenderFillRect(global_handler->renderer, nullptr);

roundedBoxRGBA(global_handler->renderer, 0, 0, width, height, radius, getRed(fillColor), getGreen(fillColor), getBlue(fillColor), 255);
roundedRectangleRGBA(global_handler->renderer, 0, 0, width-1, height-1, radius, getRed(borderColor), getGreen(borderColor), getBlue(borderColor), 255);

return Graphics::Bitmap(texture);
}

Graphics::Bitmap::Bitmap(){
setData(std::shared_ptr<BitmapData>(new BitmapData(nullptr, nullptr)));
}
Expand Down Expand Up @@ -703,6 +718,41 @@ void Graphics::Bitmap::StretchHqx(const Bitmap & where, const int sourceX, const
}

void Graphics::Bitmap::Stretch(const Bitmap & where, const int sourceX, const int sourceY, const int sourceWidth, const int sourceHeight, const int destX, const int destY, const int destWidth, const int destHeight ) const {
SDL_Texture* texture = getTexture(false);

if (texture != nullptr){
where.activate();
SDL_Rect destRect;
destRect.x = destX + where.clip_x1;
destRect.y = destY + where.clip_y1;
// SDL_Point size;
// FIXME: cache the texture size
// SDL_QueryTexture(this->getData()->texture, NULL, NULL, &size.x, &size.y);
destRect.w = destWidth;
destRect.h = destHeight;
// DebugLog << "draw size is " << size.x << " " << size.y << endl;

SDL_Rect sourceRect;
sourceRect.x = this->clip_x1 + sourceX;
sourceRect.y = this->clip_y1 + sourceY;

if (sourceWidth + sourceX > this->clip_x2 - this->clip_x1){
sourceRect.w = this->clip_x2 - this->clip_x1 - sourceX;
} else {
sourceRect.w = sourceWidth;
}
if (sourceHeight + sourceY > this->clip_y2 - this->clip_y1){
sourceRect.h = this->clip_y2 - this->clip_y1 - sourceY;
} else {
sourceRect.h = sourceHeight;
}

where.enableClip();
SDL_RenderCopy(global_handler->renderer, texture, &sourceRect, &destRect);
where.disableClip();

// SDL_RenderCopy(global_handler->renderer, this->getData()->texture, NULL, NULL);
}
}

void Graphics::Bitmap::StretchBy2( const Bitmap & where ){
Expand Down Expand Up @@ -828,6 +878,9 @@ void Graphics::TranslucentBitmap::rectangleFill(int x1, int y1, int x2, int y2,
}

void Graphics::TranslucentBitmap::rectangle(int x1, int y1, int x2, int y2, Color color) const {
SDL_SetRenderDrawBlendMode(global_handler->renderer, SDL_BLENDMODE_BLEND);
Graphics::Bitmap::rectangle(x1, y1, x2, y2, color.updateAlpha(alpha));
SDL_SetRenderDrawBlendMode(global_handler->renderer, SDL_BLENDMODE_NONE);
}

void Graphics::TranslucentBitmap::line( const int x1, const int y1, const int x2, const int y2, const Color color ) const {
Expand Down Expand Up @@ -916,15 +969,25 @@ int Graphics::setGraphicsMode(int mode, int width, int height){
DebugLog << "Unable to get renderer info: " << SDL_GetError() << endl;
}

SDL_RenderSetLogicalSize(renderer, width, height);
/*
double ratio = 640 / (double) 480;
if (width / ratio > height){
width = height * ratio;
} else {
height = width / ratio;
}
*/
// SDL_RenderSetLogicalSize(renderer, width, height);
/* always render at 640x480 resolution */
SDL_RenderSetLogicalSize(renderer, 640, 480);

global_handler = unique_ptr<SDLGlobalHandler>(new SDLGlobalHandler(window, renderer));

Screen = new Bitmap();
Screen->clip_x1 = 0;
Screen->clip_y1 = 0;
Screen->clip_x2 = width;
Screen->clip_y2 = height;
Screen->clip_x2 = 640;
Screen->clip_y2 = 480;

return 0;
}
Expand Down Expand Up @@ -975,6 +1038,17 @@ void Graphics::StretchedBitmap::hLine(const int x1, const int y, const int x2, c
*/

int Graphics::changeGraphicsMode(int mode, int width, int height){
/*
double ratio = 640 / (double) 480;
if (width / ratio > height){
width = height * ratio;
} else {
height = width / ratio;
}
*/
// SDL_RenderSetLogicalSize(global_handler->renderer, width, height);

// SDL_RenderSetLogicalSize(global_handler->renderer, width, height);
return 0;
}

Expand Down
30 changes: 30 additions & 0 deletions src/r-tech1/graphics/texture-cache.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "texture-cache.h"
#include "graphics/bitmap.h"

namespace Graphics {

Util::Parameter<std::shared_ptr<TextureCache>> TextureCache::cache;

TextureCache::TextureCache(){
}

TextureCache::~TextureCache(){
}

std::shared_ptr<Graphics::Bitmap> TextureCache::getTexture(const std::string & key, std::function<std::shared_ptr<Graphics::Bitmap>()> create){

std::map<std::string, std::shared_ptr<Graphics::Bitmap>>::iterator it = textures.find(key);
if (it == textures.end()){
std::shared_ptr<Graphics::Bitmap> bitmap = create();
textures[key] = bitmap;
return bitmap;
}

return it->second;
}

LocalTextureCache::LocalTextureCache():
local(TextureCache::cache, std::shared_ptr<TextureCache>(new TextureCache())){
}

}
32 changes: 32 additions & 0 deletions src/r-tech1/graphics/texture-cache.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#pragma once

#include "r-tech1/parameter.h"
#include <memory>
#include <functional>
#include <map>
#include "bitmap.h"

namespace Graphics {

class TextureCache{
public:
static Util::Parameter<std::shared_ptr<TextureCache>> cache;

TextureCache();
virtual ~TextureCache();

std::shared_ptr<Graphics::Bitmap> getTexture(const std::string & key, std::function<std::shared_ptr<Graphics::Bitmap>()> create);

private:
std::map<std::string, std::shared_ptr<Graphics::Bitmap>> textures;
};

class LocalTextureCache {
public:
LocalTextureCache();

private:
Util::Parameter<std::shared_ptr<TextureCache>> local;
};

}
18 changes: 16 additions & 2 deletions src/r-tech1/gui/box.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include "r-tech1/graphics/bitmap.h"
#include "r-tech1/graphics/texture-cache.h"
#include "r-tech1/gui/box.h"

#include "r-tech1/font.h"
// #include "r-tech1/debug.h"

using namespace Gui;

Expand Down Expand Up @@ -35,12 +36,25 @@ void Box::render(const Graphics::Bitmap & work){
Util::ReferenceCount<Graphics::Bitmap> workArea = checkWorkArea(work);
if (workArea != NULL){
// Graphics::Bitmap::transBlender(0, 0, 0, colors.bodyAlpha);
const Graphics::TranslucentBitmap area = workArea->translucent(colors.bodyAlpha);
// Check if we are using a rounded box
if (transforms.getRadius() > 0){

std::string key = std::string(__FILE__) + ":" + std::to_string(__LINE__) + ":" + std::to_string(workArea->getWidth()) + ":" + std::to_string(workArea->getHeight());

// Graphics::Bitmap round = Graphics::makeRoundedRect(workArea->getWidth(), workArea->getHeight(), transforms.getRadius(), colors.body, colors.border);
std::shared_ptr<Graphics::Bitmap> round = Graphics::TextureCache::cache.current()->getTexture(key, [&]{
// DebugLog << "Generate box texture" << std::endl;
return std::shared_ptr<Graphics::Bitmap>(new Graphics::Bitmap(Graphics::makeRoundedRect(workArea->getWidth(), workArea->getHeight(), transforms.getRadius(), colors.body, colors.border)));
});

round->translucent(colors.bodyAlpha).draw(0, 0, *workArea);

/*
area.roundRectFill((int)transforms.getRadius(), 0, 0, area.getWidth() - 1, area.getHeight()-1, colors.body);
area.roundRect((int)transforms.getRadius(), 0, 0, area.getWidth()-1, area.getHeight()-1, colors.border);
*/
} else {
const Graphics::TranslucentBitmap area = workArea->translucent(colors.bodyAlpha);
area.rectangleFill(0, 0, area.getWidth()-1, area.getHeight()-1, colors.body);
area.rectangle(0, 0, area.getWidth()-1, location.getHeight()-1, colors.border);
}
Expand Down
3 changes: 3 additions & 0 deletions src/r-tech1/menu/menu.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "r-tech1/graphics/bitmap.h"
#include "r-tech1/graphics/texture-cache.h"
#include "r-tech1/menu/menu.h"
#include "r-tech1/menu/menu_option.h"
#include "r-tech1/version.h"
Expand Down Expand Up @@ -1410,6 +1411,8 @@ void Menu::Menu::run(const Context & parentContext){
// Setup context from parent and this menu and initialize
Context localContext(parentContext, context);
Util::Parameter<Util::ReferenceCount<FontInfo> > currentFont(menuFontParameter);
/* create a local texture cache for this menu */
Graphics::LocalTextureCache textureCache;
if (context.hasFont()){
currentFont.push(context.getFontInfo());
}
Expand Down
1 change: 1 addition & 0 deletions src/r-tech1/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ sources = [
'exceptions/exception.cpp',
'graphics/bitmap.cpp',
'graphics/gradient.cpp',
'graphics/texture-cache.cpp',
'system.cpp',
'version.cpp',
'argument.cpp',
Expand Down

0 comments on commit cc2a419

Please sign in to comment.