Advertisement
Guest User

SDL2 Debug for Stackoverflow

a guest
Dec 26th, 2019
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.91 KB | None | 0 0
  1. #include "program.h"
  2. #include<iostream>
  3. #include<string> // for string class
  4. #include <chrono>
  5. #include <thread>
  6.  
  7. struct SDL_SW_YUVTexture
  8. {
  9.     Uint32 format;
  10.     Uint32 target_format;
  11.     int w, h;
  12.     Uint8 *pixels;
  13.  
  14.     /* These are just so we don't have to allocate them separately */
  15.     Uint16 pitches[3];
  16.     Uint8 *planes[3];
  17.  
  18.     /* This is a temporary surface in case we have to stretch copy */
  19.     SDL_Surface *stretch;
  20.     SDL_Surface *display;
  21. };
  22.  
  23. typedef enum
  24. {
  25.     SDL_ScaleModeNearest,
  26.     SDL_ScaleModeLinear,
  27.     SDL_ScaleModeBest
  28. } SDL_ScaleMode;
  29.  
  30. struct SDL_Texture
  31. {
  32.     const void *magic;
  33.     Uint32 format;              /**< The pixel format of the texture */
  34.     int access;                 /**< SDL_TextureAccess */
  35.     int w;                      /**< The width of the texture */
  36.     int h;                      /**< The height of the texture */
  37.     int modMode;                /**< The texture modulation mode */
  38.     SDL_BlendMode blendMode;    /**< The texture blend mode */
  39.     SDL_ScaleMode scaleMode;    /**< The texture scale mode */
  40.     Uint8 r, g, b, a;           /**< Texture modulation values */
  41.  
  42.     SDL_Renderer *renderer;
  43.  
  44.     /* Support for formats not supported directly by the renderer */
  45.     SDL_Texture *native;
  46.     SDL_SW_YUVTexture *yuv;
  47.     void *pixels;
  48.     int pitch;
  49.     SDL_Rect locked_rect;
  50.  
  51.     Uint32 last_command_generation; /* last command queue generation this texture was in. */
  52.  
  53.     void *driverdata;           /**< Driver specific texture representation */
  54.  
  55.     SDL_Texture *prev;
  56.     SDL_Texture *next;
  57. };
  58.  
  59. // label class declaration
  60. class label
  61. {
  62.     friend class panel;
  63.     public:
  64.         label(SDL_Window* const w, TTF_Font* const fnt, SDL_Rect rect);
  65.  
  66.         int draw();
  67.         int draw(const std::string& txt, SDL_Color fgCol, int quality);
  68.         int setText(const std::string &txt, SDL_Color fgCol, int quality);
  69.  
  70.     public:
  71.         std::string m_txt;
  72.         TTF_Font *const m_font = nullptr;
  73.         SDL_Surface *m_surf = nullptr;
  74.         SDL_Texture *m_texture = nullptr;
  75.         SDL_Rect m_rect{0, 0, 0, 0};
  76.         SDL_Renderer *m_ren;
  77.         SDL_Window *m_wnd;
  78.         Uint32 m_pixForm;
  79. };
  80.  
  81. // label class constructor
  82. 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)
  83. {
  84. // save window info locally for label object
  85.     m_ren =SDL_GetRenderer(m_wnd);
  86.     m_pixForm =SDL_GetWindowPixelFormat(m_wnd);
  87.     m_texture = SDL_CreateTexture(m_ren, m_pixForm,SDL_TEXTUREACCESS_STATIC, 500, 300);
  88.     m_surf = nullptr;
  89.     std::cout << "PixelFormat " << m_pixForm << " : " << SDL_GetPixelFormatName(m_pixForm) <<  std::endl;
  90. }
  91.  
  92. // label class methods to render/draw text on screen
  93. int label::setText(const std::string& txt, SDL_Color fgCol, int quality=0)
  94. {
  95.     int w,h;
  96.     SDL_Surface *ts;
  97.     if(m_surf!=nullptr)SDL_FreeSurface(m_surf);
  98.     switch(quality)
  99.     {
  100.         case 1:
  101.         {
  102.             ts = TTF_RenderText_Shaded(m_font, txt.c_str(), fgCol, SDL_Color{0,0,0,0});
  103.             break;
  104.         }
  105.         case 2:
  106.         {
  107.             ts = TTF_RenderText_Blended(m_font,txt.c_str(), fgCol);
  108.             break;
  109.         }
  110.         default:
  111.         {
  112.             ts = TTF_RenderText_Solid(m_font, txt.c_str(), fgCol);
  113.             break;
  114.         }
  115.     }
  116.     //std::cout << "Before pixFormat ts " << SDL_GetPixelFormatName(ts->format->format)<< std::endl;
  117.     //SDL_SetSurfaceAlphaMod(ts, 0);
  118.     //SDL_SetSurfaceBlendMode(ts, SDL_BLENDMODE_BLEND);
  119.     m_surf = SDL_ConvertSurfaceFormat(ts, SDL_PIXELFORMAT_ARGB8888, 0);
  120.     //SDL_SetSurfaceAlphaMod(m_surf, 0);
  121.     //std::cout << "After pixFormat surf : " <<  SDL_GetPixelFormatName(m_surf->format->format) << " ts " << SDL_GetPixelFormatName(ts->format->format)<< std::endl;
  122.     SDL_FreeSurface(ts);
  123.     TTF_SizeText(m_font, txt.c_str(), &w, &h);
  124.     //std::cout << "Set text '" << txt << "' w: " << w << " h: " << h << std::endl;
  125.     return 1;
  126. }
  127.  
  128. int label::draw()
  129. {
  130.     // HERE is the one that draw the noise on screen!
  131.     SDL_Rect SurfRect;
  132.  
  133.     SurfRect.x = 0;
  134.     SurfRect.y = 0;
  135.     SurfRect.w = m_surf->w;
  136.     SurfRect.h = m_surf->h;
  137.     {
  138.         int rc = SDL_UpdateTexture(m_texture, &SurfRect, m_surf->pixels, m_surf->pitch);
  139.         {
  140.             Uint8 r, g, b, a;
  141.             SDL_BlendMode blendMode;
  142.             SDL_GetSurfaceColorMod(m_surf, &r, &g, &b);
  143.             SDL_SetTextureColorMod(m_texture, r, g, b);
  144.  
  145.             SDL_GetSurfaceAlphaMod(m_surf, &a);
  146.             SDL_SetTextureAlphaMod(m_texture, a);
  147.  
  148.             if (SDL_HasColorKey(m_surf)) {
  149.                 /* We converted to a texture with alpha format */
  150.                 SDL_SetTextureBlendMode(m_texture, SDL_BLENDMODE_BLEND);
  151.             } else {
  152.                 SDL_GetSurfaceBlendMode(m_surf, &blendMode);
  153.                 SDL_SetTextureBlendMode(m_texture, blendMode);
  154.             }
  155.         }
  156.         std::cout << "here"<<std::endl;
  157.         if(rc)
  158.         {
  159.             std::cout << "Error updating texture " << rc <<std::endl;
  160.             return(rc);
  161.         }
  162.     }
  163.     // if it is replaced with the following it works:
  164.  
  165.     /*{
  166.         if(m_texture!=nullptr)SDL_DestroyTexture(m_texture);
  167.         m_texture = SDL_CreateTextureFromSurface(m_ren, m_surf);
  168.     }*/
  169.  
  170.  
  171.     SDL_RenderCopy(m_ren, m_texture, NULL, NULL);
  172.     return 1;
  173. }
  174.  
  175. int label::draw(const std::string& txt, SDL_Color fgCol, int q)
  176. {
  177.     setText(txt,fgCol,q);
  178.     draw();
  179.     std::cout << "Here2" <<std::endl;
  180.     return 1;
  181. }
  182.  
  183. static int  loop(SDL_Window *wnd, SDL_Renderer *wrend) {
  184.     // Main loop
  185.     bool running = true;
  186.     SDL_Event event;
  187.     while(running) {
  188.         // Process events
  189.         while(SDL_PollEvent(&event)) {
  190.             if(event.type == SDL_QUIT) {
  191.                 running = false;
  192.             } else if(event.type == SDL_KEYDOWN) {
  193.  
  194.             }
  195.         }
  196.  
  197.         // Clear screen
  198.        // SDL_RenderClear(wrend);
  199.  
  200.         // Draw
  201.  
  202.         // Show what was drawn
  203.         SDL_RenderPresent(wrend);
  204.     }
  205.  
  206.     // Release resources
  207.     SDL_DestroyRenderer(wrend);
  208.     SDL_DestroyWindow(wnd);
  209.     SDL_Quit();
  210.     //std::chrono::duration<int, std::milli> timespan(10000);
  211.     //std::this_thread::sleep_for(timespan);
  212.     return (1);
  213. }
  214.  
  215. int         main(void) {
  216.     SDL_Window *wnd ;
  217.     SDL_Renderer *wrend;
  218. if (SDL_Init(SDL_INIT_VIDEO) == -1) { return (1); }
  219.     wnd = SDL_CreateWindow("TestWin",SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,500,300,SDL_WINDOW_SHOWN);
  220.     wrend = SDL_CreateRenderer(wnd,-1,SDL_RENDERER_SOFTWARE);
  221.     SDL_SetRenderDrawBlendMode(wrend,SDL_BLENDMODE_BLEND);
  222.     SDL_SetRenderDrawColor(wrend,0,0,0,255);
  223.     SDL_RenderClear(wrend);
  224.  
  225.     SDL_Color fcol{255,255,255, 255};
  226.     SDL_Rect lblBox;
  227.     lblBox.h = 96;
  228.     lblBox.w = 298;
  229.     lblBox.x = 0;
  230.     lblBox.y =0;
  231.  
  232.     if (TTF_Init() == -1) { return (1); }
  233.     label lbl(wnd, TTF_OpenFont(WINDOW_FONT_PATH, 68), lblBox); // create a label (getCurrentFont returns a valid TTF_Font pointer)
  234.     lbl.draw(std::string("Text Test"), fcol, 2);
  235.     SDL_RenderPresent(wrend);
  236.     SDL_ShowWindow(wnd);
  237.     std::cout << "Here7" <<std::endl;
  238.     return (loop(wnd, wrend));
  239. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement