I am getting this access violation. I use SDL2 obviously. I tried to access the pixel in the middle of the screen and set that one to white. I have tried to use other than Uint32
but that did not help. Maybe something is wrong with the order of colours in void Screen::setPixel (int x, int y, Uint8 red, Uint8 green, Uint8 blue)
?
Class:
#include "Screen.h"
namespace SebProj {
Screen::Screen() : m_window(NULL), m_renderer(NULL), m_texture(NULL), m_buffer(NULL) {
}
bool Screen::init() {
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
std::cout << "SDL Init failed" << std::endl;
return false;
}
m_window = SDL_CreateWindow("Particle Fire Explosion", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGTH, SDL_WINDOW_SHOWN);
if (m_window == NULL) {
SDL_Quit();
return false;
}
m_renderer = SDL_CreateRenderer(m_window, -1, SDL_RENDERER_PRESENTVSYNC);
m_texture = SDL_CreateTexture(m_renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, SCREEN_WIDTH, SCREEN_HEIGTH);
if (m_renderer == NULL) {
std::cout << "Could nor create renderer" << std::endl;
SDL_DestroyWindow(m_window);
SDL_Quit();
return false;
}
if (m_texture == NULL) {
std::cout << "Could not create texture" << std::endl;
SDL_DestroyRenderer(m_renderer);
SDL_DestroyWindow(m_window);
SDL_Quit();
return false;
}
Uint32* buffer = new Uint32[SCREEN_WIDTH * SCREEN_HEIGTH];
memset(buffer, 0, SCREEN_WIDTH * SCREEN_HEIGTH * sizeof(Uint32)); // memset: Set a block of memory with a particular value
// buffer[30000] = 0xFFFFFFFF; (is changing a single pixel
for (int i = 0; i < SCREEN_WIDTH * SCREEN_HEIGTH; i++) {
buffer[i] = 0xFF; // 0x0000FFFF: hexadecimal, because you only need two digits to specify one single byte
}
return true;
}
void Screen::setPixel(int x, int y, Uint8 red, Uint8 green, Uint8 blue) {
Uint32 colour = 0;
colour += red;
colour <<= 8;
colour += green;
colour <<= 8;
colour += blue;
colour <<= 8;
colour += 0xFF;
m_buffer[(y * SCREEN_WIDTH) + x] = colour;
}
void Screen::update() {
SDL_UpdateTexture(m_texture, NULL, m_buffer, SCREEN_WIDTH * sizeof(Uint32));
SDL_RenderClear(m_renderer);
SDL_RenderCopy(m_renderer, m_texture, NULL, NULL);
SDL_RenderPresent(m_renderer);
}
bool Screen::processEvents() {
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
return false;
}
}
return true;
}
void Screen:: close() {
delete[] m_buffer;
SDL_DestroyRenderer(m_renderer);
SDL_DestroyTexture(m_texture);
SDL_DestroyWindow(m_window);
SDL_Quit();
}
}
Main source file:
// Red: 0x00ff0000
// Green: 0x0000ff00
// Blue: 0x000000ff
// Yellow: 0x00ffff00
#include "Screen.h" // Brings <iostream> and "SDL.h"
#undef main
int main()
{
SebProj::Screen screen; // Or "using namespace SebProj" before the main function
if(!screen.init()) {
std::cout << "Error initialising SDL." << std::endl;
}
int max = 0;
while (true) {
// Draw Particles
// Update Particles
for (int y = 0; y < SebProj::Screen::SCREEN_HEIGTH; y++) {
for (int x = 0; x < SebProj::Screen::SCREEN_WIDTH; x++) {
screen.setPixel(x, y, 128, 0, 255);
}
}
screen.setPixel(400, 300, 255, 255, 255);
// Draw the screen
screen.update();
// Check for messages/events
if(screen.processEvents() == false) {
break;
}
}
screen.close();
return 0;
}
User contributions licensed under CC BY-SA 3.0