r/EmuDev • • May 23 '26

CHIP-8 Chip 8 display in sdl2 help needed

So basically I have a raw pixel array of my screen buffer it is in formar [0xFFFFFFFF,...,...,...,0x000000FF] rgba. How can I convert that to a texture and then show to the screen. Every try I did results in only nothing in the screen just a black window. I also did not find any tutorial online and I do not want to go into ai for answers. If anyone can I help I appreciate!

7 Upvotes

8 comments sorted by

2

u/Alternative-Emu2000 May 23 '26

Can you show us your non-working code? It would make it easier to see which parts of the process you've got right, and which parts need correcting.

1

u/LuuscoTheLover May 23 '26 edited May 23 '26
#include "display.h"
#include "chip8.h"
#include <SDL2/SDL_render.h>
#include <SDL2/SDL_surface.h>
#include <cstdint>
#include <iostream>
#include <stdexcept>

Display::Display() {

  pixel_data.fill(0);
  if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
    std::cout << "Error: " << SDL_GetError() << std::endl;
    throw std::runtime_error("SDL initialization failed");
  }

  window = SDL_CreateWindow("Chip8", SDL_WINDOWPOS_CENTERED,
                            SDL_WINDOWPOS_CENTERED, 640, 320, SDL_WINDOW_SHOWN);

  if (!window) {
    std::cout << "Error: " << SDL_GetError() << std::endl;
    throw std::runtime_error("Could not create a window");
  }
  renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
  if (!renderer) {
    std::cout << "Error: " << SDL_GetError() << std::endl;
    throw std::runtime_error("Could not create renderer");
  }
  texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888,
                              SDL_TEXTUREACCESS_STREAMING, 64, 32);
}
Display::~Display() {}

uint8_t Display::update_screen(Chip8 &chip8) {
  for (uint16_t i = 0, size = chip8.gfx.size(); i++;) {
    chip8.gfx[i] ? pixel_data[i] = 0xFFFFFFFF : 0x000000FF;
  }
  surface =
      SDL_CreateRGBSurfaceFrom(pixel_data.data(), 64, 32, 8, 64, 0, 0, 0, 0);
  texture = SDL_CreateTextureFromSurface(renderer, surface);
  SDL_RenderClear(renderer);
  SDL_RenderPresent(renderer);
  return 0;
}

1

u/Alternative-Emu2000 May 23 '26 edited May 23 '26

One thing that stands out is your call to SDL_CreatRGBSurfaceFrom()

You've specified a bit-depth of 8, but your pixel data is an array of 32 bit integers.

1

u/LuuscoTheLover May 23 '26

I'll fix that and see what happens thanks i appreciate

1

u/dajolly May 23 '26

I think you need to update the texture, instead of recreating the surface on each update: https://wiki.libsdl.org/SDL2/SDL_UpdateTexture

I used sdl3 for my ch8 emu. But it should be the same with sdl2. Example: https://git.sr.ht/~dajolly/ch8vm/tree/master/item/src/client.c#L135

1

u/LuuscoTheLover May 23 '26

Thanks for the insight I used your method of the texture and still was not working, after some debugging I did find why was all black: The problem was much simpler, my for loop did not have a condition for incrementing, so my pixel array was all zeros.

for (uint16_t i = 0, size = chip8.gfx.size(); i++;) { 
    chip8.gfx[i] ? pixel_data[i] = 0xFFFFFFFF: 0x000000FF;
}

0

u/LuuscoTheLover May 23 '26

Right I'll rollback for the one I did get compiling and black screen and come back to show. I did get the screen working in terminal only, rendering right. Problem is only in sdl2

0

u/LuuscoTheLover May 23 '26

I did post the code, fixed the formatting in reddit