07-18-2018, 08:11 PM
Oh $*%)#... yes I forgot to put TLN_Init() back after commenting out the framebuffer stuff...
Ok I've made those changes, now it runs, but when I quit the loop, it again prints "Segmentation fault" at the console. So it's great that it's running now, but an error on quit is obviously not good.
And do I need to "SDL_RenderPresent" at the end?
Here's my current code;
Ok I've made those changes, now it runs, but when I quit the loop, it again prints "Segmentation fault" at the console. So it's great that it's running now, but an error on quit is obviously not good.
And do I need to "SDL_RenderPresent" at the end?
Here's my current code;
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;
}
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);
*/
TLN_Init (hres, vres, 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_DestroyWindow( window );
//Quit SDL subsystems
SDL_Quit();
return 0;
}