Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "program.h"
- #include<iostream>
- #include<string> // for string class
- #include <chrono>
- #include <thread>
- struct SDL_SW_YUVTexture
- {
- Uint32 format;
- Uint32 target_format;
- int w, h;
- Uint8 *pixels;
- /* These are just so we don't have to allocate them separately */
- Uint16 pitches[3];
- Uint8 *planes[3];
- /* This is a temporary surface in case we have to stretch copy */
- SDL_Surface *stretch;
- SDL_Surface *display;
- };
- typedef enum
- {
- SDL_ScaleModeNearest,
- SDL_ScaleModeLinear,
- SDL_ScaleModeBest
- } SDL_ScaleMode;
- struct SDL_Texture
- {
- const void *magic;
- Uint32 format; /**< The pixel format of the texture */
- int access; /**< SDL_TextureAccess */
- int w; /**< The width of the texture */
- int h; /**< The height of the texture */
- int modMode; /**< The texture modulation mode */
- SDL_BlendMode blendMode; /**< The texture blend mode */
- SDL_ScaleMode scaleMode; /**< The texture scale mode */
- Uint8 r, g, b, a; /**< Texture modulation values */
- SDL_Renderer *renderer;
- /* Support for formats not supported directly by the renderer */
- SDL_Texture *native;
- SDL_SW_YUVTexture *yuv;
- void *pixels;
- int pitch;
- SDL_Rect locked_rect;
- Uint32 last_command_generation; /* last command queue generation this texture was in. */
- void *driverdata; /**< Driver specific texture representation */
- SDL_Texture *prev;
- SDL_Texture *next;
- };
- // label class declaration
- class label
- {
- friend class panel;
- public:
- label(SDL_Window* const w, TTF_Font* const fnt, SDL_Rect rect);
- int draw();
- int draw(const std::string& txt, SDL_Color fgCol, int quality);
- int setText(const std::string &txt, SDL_Color fgCol, int quality);
- public:
- std::string m_txt;
- TTF_Font *const m_font = nullptr;
- SDL_Surface *m_surf = nullptr;
- SDL_Texture *m_texture = nullptr;
- SDL_Rect m_rect{0, 0, 0, 0};
- SDL_Renderer *m_ren;
- SDL_Window *m_wnd;
- Uint32 m_pixForm;
- };
- // label class constructor
- label::label(SDL_Window* const w, TTF_Font* const fnt, SDL_Rect rect):m_font(fnt),m_surf(nullptr),m_rect(rect),m_wnd(w)
- {
- // save window info locally for label object
- m_ren =SDL_GetRenderer(m_wnd);
- m_pixForm =SDL_GetWindowPixelFormat(m_wnd);
- m_texture = SDL_CreateTexture(m_ren, m_pixForm,SDL_TEXTUREACCESS_STATIC, 500, 300);
- m_surf = nullptr;
- std::cout << "PixelFormat " << m_pixForm << " : " << SDL_GetPixelFormatName(m_pixForm) << std::endl;
- }
- // label class methods to render/draw text on screen
- int label::setText(const std::string& txt, SDL_Color fgCol, int quality=0)
- {
- int w,h;
- SDL_Surface *ts;
- if(m_surf!=nullptr)SDL_FreeSurface(m_surf);
- switch(quality)
- {
- case 1:
- {
- ts = TTF_RenderText_Shaded(m_font, txt.c_str(), fgCol, SDL_Color{0,0,0,0});
- break;
- }
- case 2:
- {
- ts = TTF_RenderText_Blended(m_font,txt.c_str(), fgCol);
- break;
- }
- default:
- {
- ts = TTF_RenderText_Solid(m_font, txt.c_str(), fgCol);
- break;
- }
- }
- //std::cout << "Before pixFormat ts " << SDL_GetPixelFormatName(ts->format->format)<< std::endl;
- //SDL_SetSurfaceAlphaMod(ts, 0);
- //SDL_SetSurfaceBlendMode(ts, SDL_BLENDMODE_BLEND);
- m_surf = SDL_ConvertSurfaceFormat(ts, SDL_PIXELFORMAT_ARGB8888, 0);
- //SDL_SetSurfaceAlphaMod(m_surf, 0);
- //std::cout << "After pixFormat surf : " << SDL_GetPixelFormatName(m_surf->format->format) << " ts " << SDL_GetPixelFormatName(ts->format->format)<< std::endl;
- SDL_FreeSurface(ts);
- TTF_SizeText(m_font, txt.c_str(), &w, &h);
- //std::cout << "Set text '" << txt << "' w: " << w << " h: " << h << std::endl;
- return 1;
- }
- int label::draw()
- {
- // HERE is the one that draw the noise on screen!
- SDL_Rect SurfRect;
- SurfRect.x = 0;
- SurfRect.y = 0;
- SurfRect.w = m_surf->w;
- SurfRect.h = m_surf->h;
- {
- int rc = SDL_UpdateTexture(m_texture, &SurfRect, m_surf->pixels, m_surf->pitch);
- {
- Uint8 r, g, b, a;
- SDL_BlendMode blendMode;
- SDL_GetSurfaceColorMod(m_surf, &r, &g, &b);
- SDL_SetTextureColorMod(m_texture, r, g, b);
- SDL_GetSurfaceAlphaMod(m_surf, &a);
- SDL_SetTextureAlphaMod(m_texture, a);
- if (SDL_HasColorKey(m_surf)) {
- /* We converted to a texture with alpha format */
- SDL_SetTextureBlendMode(m_texture, SDL_BLENDMODE_BLEND);
- } else {
- SDL_GetSurfaceBlendMode(m_surf, &blendMode);
- SDL_SetTextureBlendMode(m_texture, blendMode);
- }
- }
- std::cout << "here"<<std::endl;
- if(rc)
- {
- std::cout << "Error updating texture " << rc <<std::endl;
- return(rc);
- }
- }
- // if it is replaced with the following it works:
- /*{
- if(m_texture!=nullptr)SDL_DestroyTexture(m_texture);
- m_texture = SDL_CreateTextureFromSurface(m_ren, m_surf);
- }*/
- SDL_RenderCopy(m_ren, m_texture, NULL, NULL);
- return 1;
- }
- int label::draw(const std::string& txt, SDL_Color fgCol, int q)
- {
- setText(txt,fgCol,q);
- draw();
- std::cout << "Here2" <<std::endl;
- return 1;
- }
- static int loop(SDL_Window *wnd, SDL_Renderer *wrend) {
- // Main loop
- bool running = true;
- SDL_Event event;
- while(running) {
- // Process events
- while(SDL_PollEvent(&event)) {
- if(event.type == SDL_QUIT) {
- running = false;
- } else if(event.type == SDL_KEYDOWN) {
- }
- }
- // Clear screen
- // SDL_RenderClear(wrend);
- // Draw
- // Show what was drawn
- SDL_RenderPresent(wrend);
- }
- // Release resources
- SDL_DestroyRenderer(wrend);
- SDL_DestroyWindow(wnd);
- SDL_Quit();
- //std::chrono::duration<int, std::milli> timespan(10000);
- //std::this_thread::sleep_for(timespan);
- return (1);
- }
- int main(void) {
- SDL_Window *wnd ;
- SDL_Renderer *wrend;
- if (SDL_Init(SDL_INIT_VIDEO) == -1) { return (1); }
- wnd = SDL_CreateWindow("TestWin",SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,500,300,SDL_WINDOW_SHOWN);
- wrend = SDL_CreateRenderer(wnd,-1,SDL_RENDERER_SOFTWARE);
- SDL_SetRenderDrawBlendMode(wrend,SDL_BLENDMODE_BLEND);
- SDL_SetRenderDrawColor(wrend,0,0,0,255);
- SDL_RenderClear(wrend);
- SDL_Color fcol{255,255,255, 255};
- SDL_Rect lblBox;
- lblBox.h = 96;
- lblBox.w = 298;
- lblBox.x = 0;
- lblBox.y =0;
- if (TTF_Init() == -1) { return (1); }
- label lbl(wnd, TTF_OpenFont(WINDOW_FONT_PATH, 68), lblBox); // create a label (getCurrentFont returns a valid TTF_Font pointer)
- lbl.draw(std::string("Text Test"), fcol, 2);
- SDL_RenderPresent(wrend);
- SDL_ShowWindow(wnd);
- std::cout << "Here7" <<std::endl;
- return (loop(wnd, wrend));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement