LAK132 icon

TFT ImGui

LAK132 | PRO | 12/01/21 07:18:23 AM UTC (Edited) | 0 ⭐ | 1055 👁️ | Never ⏰ | []
C++ |

6.41 KB

|

None

|

0 👍

/

0 👎

#include "imgui.h"
 
#define TFTX 220
#define TFTY 176
 
#include "SPI.h"
#include <TFT_22_ILI9225.h>
 
const uint8_t HSPICLK = 14;
const uint8_t HSPICS0 = 15;
const uint8_t HSPIMISO = 12;
const uint8_t HSPIMOSI = 13;
const uint8_t HSPIHD = 4;
const uint8_t HSPIWP = 2;
 
const uint8_t ADC2CH1 = 0; 
const uint8_t BOOTBTN = ADC2CH1;
const uint8_t LEDPIN = 16;
 
const uint8_t TFTLED = 25;
const uint8_t TFTRST = 26;
const uint8_t TFTRS = 27;
const uint8_t TFTCS = HSPICS0; 
const uint8_t TFTCLK = HSPICLK; 
const uint8_t TFTSDI = HSPIMOSI; 
 
TFT_22_ILI9225 tft = TFT_22_ILI9225(TFTRST, TFTRS, TFTCS, TFTSDI, TFTCLK, TFTLED, 128);
            
float displayScale = 1.2f;
 
typedef struct fontAtlas 
{
  unsigned char* pixels;
  int width, height;
} fontAtlas_t;
 
fontAtlas_t fonts;
 
void TFTRenderFunc(ImDrawData* draw_data)
{
  for (int n = 0; n < draw_data->CmdListsCount; n++)
  {
    ImDrawList* cmd_list = *draw_data->CmdLists;
    const ImDrawVert* vtx_buffer = cmd_list->VtxBuffer.Data;
    const ImDrawIdx* idx_buffer = cmd_list->IdxBuffer.Data;
    for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++)
    {
      const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
      if (pcmd->UserCallback)
      {
        pcmd->UserCallback(cmd_list, pcmd);
        Serial.println("User callback");
      }
      else
      {
        Serial.println("Other callback");
        Serial.println((int)pcmd->TextureId);
        //MyEngineBindTexture(pcmd->TextureId);
        Serial.print("ClipRect.x ");
        Serial.println(pcmd->ClipRect.x);
        //MyEngineScissor((int)pcmd->ClipRect.x, (int)pcmd->ClipRect.y, (int)(pcmd->ClipRect.z - pcmd->ClipRect.x), (int)(pcmd->ClipRect.w - pcmd->ClipRect.y));
        Serial.print("ElemCount ");
        Serial.println(pcmd->ElemCount);
        //MyEngineDrawIndexedTriangles(pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, idx_buffer, vtx_buffer);
        //for(int i = 0; i < pcmd->ElemCount/3; i++)
        for(int i = 0; i < pcmd->ElemCount; i+=3)
        {
          for(int j = 0; j < 3; j++)
          {
            Serial.println("Color ");
            Serial.println(vtx_buffer[idx_buffer[i+j]].col, HEX);;
            
            Serial.println("UV ");
            Serial.print(vtx_buffer[idx_buffer[i+j]].uv.x);
            Serial.print(" ");
            Serial.println(vtx_buffer[idx_buffer[i+j]].uv.y);
          }
 
          uint8_t alpha = ((vtx_buffer[idx_buffer[i]].col >> IM_COL32_A_SHIFT) & 0xFF + 
            (vtx_buffer[idx_buffer[i+1]].col >> IM_COL32_A_SHIFT) & 0xFF +
            (vtx_buffer[idx_buffer[i+2]].col >> IM_COL32_A_SHIFT) & 0xFF)/3;
          if (alpha == 0x00) continue;
          uint8_t red = ((vtx_buffer[idx_buffer[i]].col >> IM_COL32_R_SHIFT) & 0xFF + 
            (vtx_buffer[idx_buffer[i+1]].col >> IM_COL32_R_SHIFT) & 0xFF +
            (vtx_buffer[idx_buffer[i+2]].col >> IM_COL32_R_SHIFT) & 0xFF)/3;
          uint8_t green = ((vtx_buffer[idx_buffer[i]].col >> IM_COL32_G_SHIFT) & 0xFF + 
            (vtx_buffer[idx_buffer[i+1]].col >> IM_COL32_G_SHIFT) & 0xFF +
            (vtx_buffer[idx_buffer[i+2]].col >> IM_COL32_G_SHIFT) & 0xFF)/3;
          uint8_t blue = ((vtx_buffer[idx_buffer[i]].col >> IM_COL32_B_SHIFT) & 0xFF + 
            (vtx_buffer[idx_buffer[i+1]].col >> IM_COL32_B_SHIFT) & 0xFF +
            (vtx_buffer[idx_buffer[i+2]].col >> IM_COL32_B_SHIFT) & 0xFF)/3;
          
          tft.fillTriangle(
            vtx_buffer[idx_buffer[i]].pos.x * displayScale, vtx_buffer[idx_buffer[i]].pos.y * displayScale, 
            vtx_buffer[idx_buffer[i+1]].pos.x * displayScale, vtx_buffer[idx_buffer[i+1]].pos.y * displayScale, 
            vtx_buffer[idx_buffer[i+2]].pos.x * displayScale, vtx_buffer[idx_buffer[i+2]].pos.y * displayScale, 
            tft.setColor(red, green, blue));
        }
      }
      idx_buffer += pcmd->ElemCount;
    }
  }
}
 
void setup()
{
  Serial.begin(115200);
  
  tft.begin();//tftspi);
  tft.setFont(Terminal6x8);
  tft.setOrientation(3);
  digitalWrite(TFTLED, HIGH);
    
  ImGui::CreateContext();
  ImGuiIO& io = ImGui::GetIO();
  io.DisplaySize.x = TFTX;
  io.DisplaySize.y = TFTY;
  io.RenderDrawListsFn = TFTRenderFunc;
  //io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
  io.Fonts->GetTexDataAsAlpha8(&(fonts.pixels), &(fonts.width), &(fonts.height));
  //MyTexture* texture = MyEngine::CreateTextureFromMemoryPixels(pixels, width, height, TEXTURE_TYPE_RGBA)
  //io.Fonts->TexID = (void*)texture;
}
 
void loop()
{
  ImGuiIO& io = ImGui::GetIO();
  io.DeltaTime = 1.0f/60.0f;
  //io.MousePos = mouse_pos;
  //io.MouseDown[0] = mouse_button_0;
  //io.MouseDown[1] = mouse_button_1;
  //[0.0f - 1.0f]
  io.NavFlags |= ImGuiNavFlags_EnableGamepad;
  io.NavInputs[ImGuiNavInput_Activate] = 0.0f;    // activate / open / toggle / tweak value       // e.g. Circle (PS4), A (Xbox), B (Switch), Space (Keyboard)
  io.NavInputs[ImGuiNavInput_Cancel] = 0.0f;      // cancel / close / exit                        // e.g. Cross  (PS4), B (Xbox), A (Switch), Escape (Keyboard)
  io.NavInputs[ImGuiNavInput_Input] = 0.0f;       // text input / on-screen keyboard              // e.g. Triang.(PS4), Y (Xbox), X (Switch), Return (Keyboard)
  io.NavInputs[ImGuiNavInput_Menu] = 0.0f;        // tap: toggle menu / hold: focus, move, resize // e.g. Square (PS4), X (Xbox), Y (Switch), Alt (Keyboard)
  io.NavInputs[ImGuiNavInput_DpadLeft] = 0.0f;    // move / tweak / resize window (w/ PadMenu)    // e.g. D-pad Left/Right/Up/Down (Gamepads), Arrow keys (Keyboard)
  io.NavInputs[ImGuiNavInput_DpadRight] = 0.0f;
  io.NavInputs[ImGuiNavInput_DpadUp] = 0.0f;
  io.NavInputs[ImGuiNavInput_DpadDown] = 0.0f;
  io.NavInputs[ImGuiNavInput_TweakSlow] = 0.0f;   // slower tweaks                                // e.g. L1 or L2 (PS4), LB or LT (Xbox), L or ZL (Switch)
  io.NavInputs[ImGuiNavInput_TweakFast] = 0.0f;   // faster tweaks                                // e.g. R1 or R2 (PS4), RB or RT (Xbox), R or ZL (Switch)
 
  ImGui::NewFrame();
  
  static float f = 0.0f;
  ImGui::Text("Hello, world!");
  ImGui::SliderFloat("float", &f, 0.0f, 1.0f);
  ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate);
 
  tft.clear();
  ImGui::Render();
  //SwapBuffers();
  Serial.print("Cool ");
  Serial.println(f);
  delay(1000);
  displayScale += 0.1f;
  if (displayScale > 1.5f) displayScale = 0.3f;
}

Comments