I trying to render a special image which is just a bunch of pixels with no format. It is a sort of a raw image with 42x 42 pixels. The images are palettes so I am curious how I should handle that situation too.
How can I convert an array of pixels to a texture in SDL2?
Do I apply palettes in the end?
More details: Currently I am opening the image and adding some transparent (black bytes) and storing this into a char array. These pixels I will need to render.
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <SDL.h>
#include <SDL_image.h>
#include <stdio.h>
#include <windows.h>
#include <vector>
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
FILE* OpenCelFile(const char * filepath) {
FILE* ptr = fopen(filepath, "rb");
return ptr;
}
DWORD FrameCount[1] = { 0 };
DWORD DataStart[1] = { 0 };
DWORD dummy[1] = { 0 };
signed char CommandByte[1] = { 0 };
void SkipToData(FILE* pFile, DWORD* dataStart, int bytesRead)
{
int MoveForwardBytes = *dataStart - bytesRead;
fseek(pFile, MoveForwardBytes, SEEK_CUR);
}
// There is more to this , but it isn't 100% important.
void ReadCelHeader(FILE* pFile)
{
fread((void*)FrameCount, 4, 1, pFile);
fread((void*)DataStart, 4, 1, pFile);
SkipToData(pFile, DataStart, 8);
}
void ReadCommandByte(FILE* pFile)
{
fread((void*)CommandByte, 1, 1, pFile);
}
std::vector<char> backBuffer;
int CreateRawImageBuffer(FILE* pFile) {
ReadCommandByte(pFile);
int bytesRead = 0;
// handel transparent bytes;
if (*(BYTE*)CommandByte >= 0x80) {
// this is a negative byte
signed int skipBytes = *(BYTE*)CommandByte - 256;
// convert it to positive number.
skipBytes = abs(skipBytes);
for (skipBytes; skipBytes != NULL; skipBytes--) {
backBuffer.push_back(0x00);
bytesRead++;
}
}
// set real pixels
if (*(BYTE*)CommandByte < 0x80) {
signed int byteCount = *(BYTE*)CommandByte;
for (byteCount; byteCount != NULL; byteCount--) {
BYTE t_char[1] = {0x00};
fread((void*)t_char, 1, 1, pFile);
backBuffer.push_back(*t_char);
bytesRead++;
}
}
return bytesRead;
}
int main(int argc, char* args[]) {
SDL_Window* window = NULL;
SDL_Surface* screenSurface = NULL;
bool RunningMainGameLoop = true;
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
fprintf(stderr, "could not initialize sdl2: %s\n", SDL_GetError());
return 1;
}
window = SDL_CreateWindow("Renderunkimage", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
if (window == NULL) {
fprintf(stderr, "could not create window: %s\n", SDL_GetError());
return 1;
}
FILE* ptr = OpenCelFile("C:\\Users\\luzer\\source\\repos\\unkimages\\unkimage.cel");
ReadCelHeader(ptr);
int TotalFramePixels = 48 * 48;
int fc = *FrameCount;
int pixelsAdded = { 0 };
for (fc; fc != 0; fc--) {
for (TotalFramePixels; TotalFramePixels != NULL; TotalFramePixels = TotalFramePixels - pixelsAdded) {
pixelsAdded = CreateRawImageBuffer(ptr);
}
}
screenSurface = SDL_GetWindowSurface(window);
SDL_FillRect(screenSurface, NULL, SDL_MapRGB(screenSurface->format, 0x00, 0x00, 0x00));
SDL_UpdateWindowSurface(window);
while (RunningMainGameLoop)
{
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
RunningMainGameLoop = false;
break;
}
}
// handle renders...
}
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}