Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TLN_SetRenderTarget parameter
#3
Yeah I think uint8_t* is clearer, I think I've seen it before to represent colour bytes. And yeah I found those parts of the code, no problem Smile but here's where I'm actually stuck...

Code:
            SDL_LockTexture (backbuffer, NULL, (void*)&rt_pixels, &rt_pitch);
            TLN_SetRenderTarget (rt_pixels, rt_pitch);
            TLN_BeginFrame (frame);

In that first line, I get a compile error "Can't convert void* to void**" so naturally the naive thing to do is change it to cast to (void**) - then it compiles, but I get a "Segmentation fault" during runtime and the program quits.

I'm actually not surprised to see this kind of error, because as far as I understand, we're using the void pointer to just start accessing the memory directly and we can easily overstep our bounds if we don't know what we're doing, which is obviously happening, looking at the error. I educated myself on void pointers recently. I get the general idea of it, but not sure what to do here specifically.

Here's the full code (not cleaned up yet);
Code:
//Using SDL and standard IO
#include<SDL2/SDL.h>
#include <stdio.h>
#include <Tilengine.h>

//Screen dimension constants
const int SCREEN_WIDTH = 320;
const int SCREEN_HEIGHT = 240;

int main( int argc, char* args[] )
{
    //The window we'll be rendering to
    SDL_Window* window = NULL;

    //The surface contained by the window
    SDL_Surface* screenSurface = NULL;

    //Initialize SDL
    if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
    {
        printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
        return 1;
    }

    //Create window
    window = SDL_CreateWindow( "Tilengine with scaling and sound", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
    if( window == NULL )
    {
        printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
        return 1;
    }

    const int hres = 400;
    const int vres = 240;
    int frame = 0;
    /*
    const int pitch = hres * sizeof(int);
    void* framebuffer;
    // init and set framebuffer
    TLN_Init (hres, vres, 2, 80, 0);
    framebuffer = malloc (pitch * vres);
    TLN_SetRenderTarget ((uint8_t*)framebuffer, pitch);
    */

    SDL_Renderer* renderer = SDL_CreateRenderer (window, -1, 0);
    uint8_t*         rt_pixels;
    int             rt_pitch;
    SDL_Texture*     backbuffer;
    backbuffer = SDL_CreateTexture (renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, SCREEN_WIDTH,SCREEN_HEIGHT);

    //Main loop flag
    bool quit = false;
    //Event handler
    SDL_Event e;
     //While application is running
    while( !quit )
    {
        //Handle events on queue
        while( SDL_PollEvent( &e ) != 0 )
        { //User requests quit
            if( e.type == SDL_QUIT )
                quit = true;
            if( e.type == SDL_KEYDOWN )
            {
                if(e.key.keysym.sym == SDLK_ESCAPE)
                    quit = true;
            }

            SDL_LockTexture (backbuffer, NULL, (void**)&rt_pixels, &rt_pitch);
            TLN_SetRenderTarget (rt_pixels, rt_pitch);
            TLN_BeginFrame (frame);

            TLN_UpdateFrame (frame);
            frame += 1;
            SDL_UnlockTexture (backbuffer);

            SDL_RenderPresent (renderer);
        }
    }

    /* deallocate */
    //free (framebuffer);
    TLN_Deinit ();

    //Destroy window
    SDL_DestroyWindow( window );
    //Quit SDL subsystems
    SDL_Quit();

    return 0;
}
Reply


Messages In This Thread
TLN_SetRenderTarget parameter - by Domarius - 07-18-2018, 01:56 AM
RE: TLN_SetRenderTarget parameter - by megamarc - 07-18-2018, 03:15 AM
RE: TLN_SetRenderTarget parameter - by Domarius - 07-18-2018, 10:27 AM
RE: TLN_SetRenderTarget parameter - by megamarc - 07-18-2018, 03:33 PM
RE: TLN_SetRenderTarget parameter - by Domarius - 07-18-2018, 08:11 PM
RE: TLN_SetRenderTarget parameter - by megamarc - 07-19-2018, 03:44 PM
RE: TLN_SetRenderTarget parameter - by Domarius - 07-19-2018, 09:25 PM
RE: TLN_SetRenderTarget parameter - by megamarc - 07-21-2018, 03:56 PM
RE: TLN_SetRenderTarget parameter - by Domarius - 07-22-2018, 12:21 AM
RE: TLN_SetRenderTarget parameter - by megamarc - 07-24-2018, 01:52 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)