Arduino TFT_eSPI 库精灵无法按预期工作

问题描述 投票:0回答:1

我正在 platform.io 中使用 Arduino 标头和 ESP32-2432S024 CYD 显示器创建一个项目。我一直在尝试创建一些精灵,最终我能够做到。问题是我必须不断地在循环中创建我的精灵对象。我在这里按照本教程添加精灵:https://www.youtube.com/watch?v=U4jOFLFNZBI&t=384s&ab_channel=VolosProjects,其中不需要这样做。

有谁知道为什么需要这样做,或者我是否做错了什么导致需要这样做。

#include "game.h"

/**
 * DONT FORGET TO CHANGE THE USER_SETUP.H IN THE TFT_eSPI lib 
 */

void Game::Init()
{
    // Start the tft display and set it to black
    tft.init();
    tft.setRotation(1); //This is the display in landscape
    tft.setSwapBytes(true);

    // Clear the screen before writing to it
    tft.fillScreen(TFT_BLACK);
}


float x = 50;
float y = 50;
void Game::Loop()
{
    background.createSprite(screenWidth, screenHeight);
    background.setSwapBytes(true); // Correct color
    background.setColorDepth(8);
    background.pushImage(0, 0, screenWidth, screenHeight, backgroundSprite);

    cursor.createSprite(16,16);
    cursor.setColorDepth(8);    
    cursor.pushImage(0, 0, 16, 16, cursorSprite);
    cursor.pushToSprite(&background, x, y, TFT_BLACK); 
    
    background.pushSprite(0,0);

    x++;
    y++;
    if (x > screenWidth) {
        x = 0;
    }
    if (y > screenHeight) {
        y = 0;
    }
}
#ifndef GAME_H
#define GAME_H

#include "Arduino.h"
#include <TFT_eSPI.h>
#include "sprites/cursor.h"
#include "sprites/background.h"

class Game 
{
    public:
        void Init();
        void Loop();
    private:
        TFT_eSPI tft = TFT_eSPI();
        TFT_eSprite cursor = TFT_eSprite(&tft);
        TFT_eSprite background = TFT_eSprite(&tft);
        TFT_eSprite gyroText = TFT_eSprite(&tft);
        float screenWidth = 320;
        float screenHeight = 240;
};

#endif

我尝试过移动:

    background.createSprite(screenWidth, screenHeight);
    background.setSwapBytes(true); // Correct color
    background.setColorDepth(8);

进入 Init() 函数,但这会导致屏幕变黑,当我尝试使用以下方法时,也会发生同样的情况:

    cursor.createSprite(16,16);
    cursor.setColorDepth(8);

当尝试将这些行中的任何一行移出循环并进入 Init 时,它会导致屏幕不再显示任何内容,而只是黑屏。

c++ arduino
1个回答
0
投票

我在这篇文章中找到了答案:TFT_eSPI.h 库的显示问题:调整尺寸后 Sprite 未正确显示

将函数 setColorDepth(8) 移至 createSprite() 函数上方似乎已修复它!

© www.soinside.com 2019 - 2024. All rights reserved.