Enter code here...//Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
// Declare Functions
// Starts up SDL and creates Window
bool init();
// Loads media
bool loadMedia();
// Fress media and shutdown SDL
void close();
//The window we'll be rendering to
SDL_Window* gWindow = NULL;
//The surface contained by the window
SDL_Surface* gScreenSurface = NULL;
//The image we will load and show on the screen
SDL_Surface* gHelloWorld = NULL;
// The Render
SDL_Renderer* gRenderer = NULL;
// The Texture
SDL_Texture* gTexture = NULL;
bool init()
{
// Initialization flag
bool success = true;
// Initialize SDL
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
success = false;
}
else
{
// Create Window
gWindow = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH,
SCREEN_HEIGHT,
SDL_WINDOW_SHOWN);
if (gWindow == NULL)
{
printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
success = false;
}
else
{
// Get window surface
gScreenSurface = SDL_GetWindowSurface(gWindow);
}
#ifdef __EMSCRIPTEN__
// Create a Rendering Wimdow
if (SDL_CreateWindowAndRenderer(SCREEN_WIDTH, SCREEN_HEIGHT,
SDL_WINDOW_RESIZABLE, &gWindow, &gRenderer))
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window and
renderer: %s", SDL_GetError());
return 3;
}
//gScreenSurface = SDL_SetVideoMode(512, 512, 32, SDL_SWSURFACE);
// printf(" Get window surface :\n");
printf("SDL initialize! :\n");
#endif
}
return success;
}
bool loadMedia()
{
// Loading sucess flag
bool success = true;
// Locad splash image
gHelloWorld = SDL_LoadBMP("hello_world.bmp");
if (gHelloWorld == NULL)
{
printf("Unable to load image %s! SDL Error: %s\n", "hello_world.bmp",
SDL_GetError());
success = false;
}
#ifdef __EMSCRIPTEN__
//Convert surface to texture befoe rendering
gTexture = SDL_CreateTextureFromSurface(gRenderer, gHelloWorld);
if (!gTexture) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture from
surface: %s", SDL_GetError());
return 3;
}
//Freedup memory
SDL_FreeSurface(gHelloWorld);
printf("finish load media \n");
#endif
return success;
}
void renderLoop()
{
// Apply the image
SDL_BlitSurface(gHelloWorld, NULL, gScreenSurface, NULL);
// Update the surface
SDL_UpdateWindowSurface(gWindow);
SDL_Delay(2000);
#ifdef __EMSCRIPTEN__
SDL_SetRenderDrawColor(gRenderer, 0x00, 0x00, 0x00, 0x00);
// clear the window
SDL_RenderClear(gRenderer);
// Copy texture to the window
SDL_RenderCopy(gRenderer, gTexture, NULL, NULL);
// Display the image
SDL_RenderPresent(gRenderer);
#endif
}
void close()
{
printf("Run Close \n");
// Deallocate surface
SDL_FreeSurface(gHelloWorld);
gHelloWorld = NULL;
// Destroy window
SDL_DestroyWindow(gWindow);
gWindow = NULL;
SDL_DestroyTexture(gTexture);
SDL_DestroyRenderer(gRenderer);
SDL_DestroyWindow(gWindow);
//Free the loaded image
SDL_FreeSurface(gHelloWorld);
// Quit SDL subsystems
SDL_Quit();
}
int main(int argc, char* args[])
{
// Start up SDL and create window
if (!init())
{
printf("Failed to initialize!\n");
}
else
{
// Load media
printf("Start LOad Media \n");
if (!loadMedia())
{
printf("Failed to load media!\n");
}
else
{
#ifdef _WIN32 // Windows system specific
renderLoop();
#endif
#ifdef __EMSCRIPTEN__
renderLoop();
#endif
}
}
// Free resources and close SDL
close();
return 0;
}
Hi
I think I got it to work
Added is my solution
On Tuesday, April 5, 2016 at 1:00:56 PM UTC+1, fmuld fhalo wrote:
>
> Hi
>
> Di anyone get this to work?
>
> On Tuesday, November 11, 2014 at 1:03:47 AM UTC, Alon Zakai wrote:
>>
>> Did you preload or embed the data files it expects? For me, without those
>> files, it builds ok and then at runtime sayd "Unable to load image ..".
>>
>> - Alon
>>
>>
>> On Sat, Nov 8, 2014 at 9:36 AM, bussiere adrien <[email protected]>
>> wrote:
>>
>>> Yeah but it worked well as intended in the binary version.
>>>
>>> I will try to dig it.
>>>
>>> If anyone have an idea.
>>>
>>> regards
>>> Bussiere
>>>
>>>
>>> Le samedi 8 novembre 2014 13:14:21 UTC+1, Bruce Mitchener a écrit :
>>>>
>>>> Yeah, I just checked the source. It is -s USE_SDL=2 ... there were
>>>> conflicting things written in some of the email threads.
>>>>
>>>> Anyway, while I don't know much (anything) about SDL, I would guess
>>>> that it is because you're not using an event loop and just blocking in
>>>> main
>>>> and SDL probably needs to run the event loop to perform the work. Couple
>>>> that with the SDL_Delay(6000) and that's probably why it waits 6 seconds
>>>> ...
>>>>
>>>> If not, someone who actually know about SDL will have to chime in. :)
>>>>
>>>> - Bruce
>>>>
>>>>
>>>> On Sat, Nov 8, 2014 at 5:38 PM, bussiere adrien <[email protected]>
>>>> wrote:
>>>>
>>>>> When i put -s USE_SDL2=1
>>>>>
>>>>> 02_getting_an_image_on_the_screen.cpp:5:10: fatal error: 'SDL2/SDL.h'
>>>>> file not
>>>>> found
>>>>> #include <SDL2/SDL.h>
>>>>> ^
>>>>> 1 error generated.
>>>>> ERROR root: compiler frontend failed to generate LLVM bitcode,
>>>>> halting
>>>>>
>>>>>
>>>>> Le samedi 8 novembre 2014 11:30:09 UTC+1, Bruce Mitchener a écrit :
>>>>>>
>>>>>> Try -s USE_SDL2=1
>>>>>>
>>>>>> Cheers,
>>>>>>
>>>>>> - Bruce
>>>>>>
>>>>>>
>>>>>> On Sat, Nov 8, 2014 at 5:00 PM, bussiere adrien <[email protected]>
>>>>>> wrote:
>>>>>>
>>>>>>> hello
>>>>>>> i would like to know if someone have time why my simple sdl2 c++
>>>>>>> program don't works as intended in html
>>>>>>> https://gist.github.com/bussiere/c9828f513ddae1f554e7
>>>>>>> it only show the second image after six seconds
>>>>>>> the compiled binary worked as intended
>>>>>>> thanks
>>>>>>>
>>>>>>> Bussiere
>>>>>>>
>>>>>>> code below :
>>>>>>>
>>>>>>> /*This source code copyrighted by Lazy Foo' Productions (2004-2013)
>>>>>>> and may not be redistributed without written permission.*/
>>>>>>> /*
>>>>>>> i compile it with :
>>>>>>>
>>>>>>> /home/bussiere/Lib/emsdk_portable/emscripten/master/./emcc
>>>>>>> 02_getting_an_image_on_the_screen.cpp -s USE_SDL=2 -s
>>>>>>> LEGACY_GL_EMULATION=1 -o sdl2.html --preload-file hello_world.bmp
>>>>>>> --preload-file hello_world2.bmp
>>>>>>>
>>>>>>> and the html file only who the second image after six econd of blank
>>>>>>>
>>>>>>>
>>>>>>> */
>>>>>>>
>>>>>>>
>>>>>>> //Using SDL and standard IO
>>>>>>> #include <SDL2/SDL.h>
>>>>>>> #include <stdio.h>
>>>>>>> #ifdef __EMSCRIPTEN__
>>>>>>> #include <emscripten.h>
>>>>>>> #endif
>>>>>>> //Screen dimension constants
>>>>>>> const int SCREEN_WIDTH = 640;
>>>>>>> const int SCREEN_HEIGHT = 480;
>>>>>>>
>>>>>>> //Starts up SDL and creates window
>>>>>>> bool init();
>>>>>>>
>>>>>>> //Loads media
>>>>>>> bool loadMedia();
>>>>>>>
>>>>>>> //Frees media and shuts down SDL
>>>>>>> void close();
>>>>>>>
>>>>>>> //The window we'll be rendering to
>>>>>>> SDL_Window* gWindow = NULL;
>>>>>>> //The surface contained by the window
>>>>>>> SDL_Surface* gScreenSurface = NULL;
>>>>>>>
>>>>>>> //The image we will load and show on the screen
>>>>>>> SDL_Surface* gHelloWorld = NULL;
>>>>>>>
>>>>>>> SDL_Surface* gHelloWorld2 = NULL;
>>>>>>>
>>>>>>> bool init()
>>>>>>> {
>>>>>>> //Initialization flag
>>>>>>> bool success = true;
>>>>>>>
>>>>>>> //Initialize SDL
>>>>>>> if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
>>>>>>> {
>>>>>>> printf( "SDL could not initialize! SDL_Error: %s\n",
>>>>>>> SDL_GetError() );
>>>>>>> success = false;
>>>>>>> }
>>>>>>> else
>>>>>>> {
>>>>>>> //Create window
>>>>>>> gWindow = SDL_CreateWindow( "SDL Tutorial",
>>>>>>> SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH,
>>>>>>> SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
>>>>>>> if( gWindow == NULL )
>>>>>>> {
>>>>>>> printf( "Window could not be created!
>>>>>>> SDL_Error: %s\n", SDL_GetError() );
>>>>>>> success = false;
>>>>>>> }
>>>>>>> else
>>>>>>> {
>>>>>>> //Get window surface
>>>>>>> gScreenSurface = SDL_GetWindowSurface( gWindow
>>>>>>> );
>>>>>>> }
>>>>>>> }
>>>>>>>
>>>>>>> return success;
>>>>>>> }
>>>>>>>
>>>>>>> bool loadMedia()
>>>>>>> {
>>>>>>> //Loading success flag
>>>>>>> bool success = true;
>>>>>>>
>>>>>>> //Load splash image
>>>>>>> gHelloWorld = SDL_LoadBMP( "hello_world.bmp" );
>>>>>>> if( gHelloWorld == NULL )
>>>>>>> {
>>>>>>> printf( "Unable to load image %s! SDL Error: %s\n",
>>>>>>> "02_getting_an_image_on_the_screen/hello_world.bmp", SDL_GetError() );
>>>>>>> success = false;
>>>>>>> }
>>>>>>>
>>>>>>> gHelloWorld2 = SDL_LoadBMP("hello_world2.bmp" );
>>>>>>> if( gHelloWorld2 == NULL )
>>>>>>> {
>>>>>>> printf( "Unable to load image %s! SDL Error: %s\n",
>>>>>>> "02_getting_an_image_on_the_screen/hello_world.bmp", SDL_GetError() );
>>>>>>> success = false;
>>>>>>> }
>>>>>>>
>>>>>>> return success;
>>>>>>> }
>>>>>>>
>>>>>>> void close()
>>>>>>> {
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> SDL_FreeSurface( gHelloWorld2 );
>>>>>>> gHelloWorld2 = NULL;
>>>>>>>
>>>>>>> //Destroy window
>>>>>>> SDL_DestroyWindow( gWindow );
>>>>>>> gWindow = NULL;
>>>>>>>
>>>>>>> //Quit SDL subsystems
>>>>>>> SDL_Quit();
>>>>>>> }
>>>>>>>
>>>>>>> int main( int argc, char* args[] )
>>>>>>> {
>>>>>>> //Start up SDL and create window
>>>>>>> if( !init() )
>>>>>>> {
>>>>>>> printf( "Failed to initialize!\n" );
>>>>>>> }
>>>>>>> else
>>>>>>> {
>>>>>>> //Load media
>>>>>>> if( !loadMedia() )
>>>>>>> {
>>>>>>> printf( "Failed to load media!\n" );
>>>>>>> }
>>>>>>> else
>>>>>>> {
>>>>>>> //Apply the image
>>>>>>> SDL_BlitSurface( gHelloWorld, NULL,
>>>>>>> gScreenSurface, NULL );
>>>>>>> //Update the surface
>>>>>>> SDL_UpdateWindowSurface( gWindow );
>>>>>>>
>>>>>>> //Wait two seconds
>>>>>>> SDL_Delay( 6000 );
>>>>>>> SDL_FreeSurface( gHelloWorld );
>>>>>>> gHelloWorld = NULL;
>>>>>>> //Apply the image
>>>>>>> SDL_BlitSurface( gHelloWorld2, NULL,
>>>>>>> gScreenSurface, NULL );
>>>>>>> //Update the surface
>>>>>>> SDL_UpdateWindowSurface( gWindow );
>>>>>>>
>>>>>>> //Wait two seconds
>>>>>>> SDL_Delay( 2000 );
>>>>>>> }
>>>>>>> }
>>>>>>>
>>>>>>> //Free resources and close SDL
>>>>>>> close();
>>>>>>>
>>>>>>> return 0;
>>>>>>> }
>>>>>>>
>>>>>>>
>>>>>>> --
>>>>>>> You received this message because you are subscribed to the Google
>>>>>>> Groups "emscripten-discuss" group.
>>>>>>> To unsubscribe from this group and stop receiving emails from it,
>>>>>>> send an email to [email protected].
>>>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>>>
>>>>>>
>>>>>> --
>>>>> You received this message because you are subscribed to the Google
>>>>> Groups "emscripten-discuss" group.
>>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>>> an email to [email protected].
>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>
>>>>
>>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "emscripten-discuss" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to [email protected].
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>>
--
You received this message because you are subscribed to the Google Groups
"emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.