CHIP-8 SDL rendering problems

714 views Asked by At

I have coded a chip-8 emulator.Whatever I do, it seems that I cannot show any pixels on the screen.The weird thing is that I have checked the code, top-bottom for 2 days already, and there does not seem to be any problem.It reads the .rom file into memory, and fetches the OP code correctly.

Here is the source code:

                SDL_SetRenderDrawColor( renderer, 0, 0, 0, SDL_ALPHA_OPAQUE );
                SDL_RenderClear(renderer);
                uint32_t pixels[(WINDOW_WIDTH / 10) * (WINDOW_HEIGHT / 10)];
                uint16_t i;
                for(i = 0; i < 64*32; i++){
                    pixels[i] = (0x00FFFFFF * display[i]) | 0xFF000000;
                }
                //upload the pixels to the texture
                SDL_UpdateTexture(tex,NULL,pixels, 64 * sizeof(uint32_t));
                //Now get the texture to the screen
                SDL_RenderCopy(renderer,tex,NULL,NULL);
                SDL_RenderPresent(renderer); // Update screen
                ch8.drawF = false;

uint16_t x = ch8->V[((ch8->opcode & 0x0F00) >> 8)];
                uint16_t y = ch8->V[((ch8->opcode & 0x00F0) >> 4)];
                uint8_t n = (ch8->opcode & 0x000F);
                for(i = 0; i < n; i++) {
                    uint8_t pixel= memory[ch8->I.word + i];
                    for(j = 0; j < 8; j++) {
                        if((pixel & (0x80 >> j)) != 0){
                            if(display[x + j  + ((y + i) * 64)] == 1) {
                                ch8->V[0xF] = 1;
                            }
                            display[x + j  + ((y + i) * 64)] ^= 1;
                        }
                    }
                }
1

There are 1 answers

0
J.Doe On

So basically, the problem was at init() function.I was initially using, SDL_CreateWindow and SDL_CreateRenderer,but now I'm using ,SDL_CreateWindowAndRenderer, which takes pointers to pointers of SDL_Window and SDL_Renderer instead of a pointer to a char and a pointer to a window.

Also there were 3 problems I fixed. 1.I was adding + 0x200 to NNN opcodes,because at firstly I thought that the NNN in ROM's are relative to 0, so I removed +0x200 from each XNNN opcode.Also I forgot a * at SDL_Texture* tex, its supposed to be SDL_Texture** tex, I was merely changing the address the local pointer was poiting too... 2.at opcode 2NNN, instead of (ch8->SP) = ch8->opcode & 0x0FFF; its(ch8->SP) = ch8->PC.word; 3.at opcode FX65 its i <= ((ch8->opcode & 0x0F00) >> 8)

Basically, the differences between SDL_CreateWindowAndRenderer and SDL_CreateWindow&SDL_CreateRenderer had me confused, I should had check'd the documentation first.

Now I only need to make the emulator only redraw the changed pixels, then make the emulator play sound.