07-19-2018, 03:44 PM
Hi again,
I've found some issues in your sample, I've fixed them and here you have a working one
I've found some issues in your sample, I've fixed them and here you have a working one
- At startup: two sets of resolutions used: 400x240 for tilengine and 320x240 for the SDL backbuffer. They must match. That was providing Tilengine a smaller work area than it needed, so buffer overflow was happening. segfault
- At deinitialize: missing SDL_DestroyTexture(backbuffer) and SDL_DestroyRenderer(renderer) before SDL_DestroyWindow(window).
- Inside the main loop: the rendering part was inside the loop that polled events on the SDL. So frames were only generated in response to SDL events, but they should be generated always (game logic doesn't wait for user input to update state).
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;
//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;
}
int frame = 0;
TLN_Init (SCREEN_WIDTH, SCREEN_HEIGHT, 2, 80, 0);
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++);
SDL_UnlockTexture (backbuffer);
SDL_RenderCopy (renderer, backbuffer, NULL, NULL);
SDL_RenderPresent (renderer);
}
/* deallocate */
//free (framebuffer);
TLN_Deinit ();
//Destroy window
SDL_DestroyTexture(backbuffer);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow( window );
//Quit SDL subsystems
SDL_Quit();
return 0;
}