指针数组,指向程序中的位图

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

我正在将位图绘制到OLED屏幕上。每个图像的格式都类似于

`const静态无符号字符waddle_dee_0 [] PROGMEM = {bits};

目前,我一直在尝试添加动画并清理代码。为此,我创建了“位图”类。

此类将存储诸如大小,宽度以及包含指向图像各帧的指针的数组之类的内容,例如

const static unsigned char* const waddle_table[] PROGMEM = {
  waddle_dee_0,
  waddle_dee_1,
  waddle_dee_2,
  waddle_dee_3,
  waddle_dee_4,
  waddle_dee_5
};

在Bitmap.cpp中,我有一个构造函数和一个函数

#include "bitmaps.h"

Bitmap::Bitmap(double w, double h, uint8_t f, size_t s, const unsigned char* const b){
    setWidth(w);
    setHeight(h);
    setFrames(f);
    setSize(s);
    setAllFrames(b);
}

void Bitmap::drawFrames(){

size_t currSize = this->getSize();
uint8_t numbOfFrames = this->getFrames();
double width = this->getWidth();
double height = this->getHeight();

Serial.println(currSize);
Serial.println(numbOfFrames);

    for (int i = 0; i < numbOfFrames; i++)
    {
      const unsigned char* frameAt = this->getSingleFrame(i);
      drawBitmap(0,0,width,height, frameAt, currSize);
      delay(100);
    }

}

并且在头文件中,我有定义

#include <stdint.h>
#include "Arduino.h"

// ensure this library description is only included once
#ifndef Bitmap_h
#define Bitmap_h

// library interface description
class Bitmap
{
  // user-accessible "public" interface
  public:
    Bitmap(double w, double h, uint8_t f, size_t s, const unsigned char* const b);
    double getWidth(){return width;}
    double getHeight(){return height;}
    uint8_t getFrames(){return frames;}
    size_t getSize(){return size;}

    double setWidth(double w){width = w;}
    double setHeight(double h){height = h;}
    uint8_t setFrames(uint8_t f){frames = f;}
    size_t setSize(size_t s){size = s;}

    //const unsigned char* const* getFrameArr(){return bitmap_frames;}
    void drawFrames();
    const unsigned char* const* getAllFrames(){return bitmap_frames;}
    void setAllFrames(const unsigned char* const b){*bitmap_frames = b;}
    const unsigned char* getSingleFrame(uint8_t f){return bitmap_frames[f];}

private:
    double width;
    double height;
    uint8_t frames;
    size_t size;
    const unsigned char* const bitmap_frames[];
};
#endif

我的问题来自指针数组,以及如何正确复制这些帧或如何将指针正确复制到新数组(bitmap_frames)。目的是使bitmap_frames与waddle_table数组相同,因此我可以遍历索引以一次依次绘制每个位图。当我只有一个硬编码数组时,我的代码可以正常工作,但是在尝试将其概括后,我遇到了许多类型错误和不一致的情况。

[如果有人可以引导我朝正确的方向前进,那么我似乎迷失在指针系统中。谢谢!

android c++ arrays pointers bitmap
1个回答
0
投票

如果您想将bitmap_frames声明为const flexible数组,则无法将其复制到它,而应在构造函数中对其进行初始化:

Bitmap::Bitmap() : bitmap_frames {waddle_dee_0, waddle_dee_1, waddle_dee_2, waddle_dee_3, waddle_dee_4, waddle_dee_5} {}

或者您可以在类定义中将bitmap_frames声明为静态的:

static const unsigned char *const bitmap_frames[];

并在源文件中定义:

const unsigned char *const Bitmap::bitmap_frames[] = {
  waddle_dee_0,
  waddle_dee_1,
  waddle_dee_2,
  waddle_dee_3,
  waddle_dee_4,
  waddle_dee_5};

也可以只使用std::vector

const std::vector<const unsigned char*> bitmap_frames;

在构造函数中:

Bitmap::Bitmap() : bitmap_frames(std::begin(waddle_table), std::end(waddle_table)) {}
© www.soinside.com 2019 - 2024. All rights reserved.