Hello,
I try to rewrite the sdl3-sample example to OpenGL ES:
#include <SDL_opengles2.h>
// ...
// draw a color
auto time = SDL_GetTicks() / 1000.f;
auto red = (std::sin(time) + 1) / 2.0;
auto green = (std::sin(time / 2) + 1) / 2.0;
auto blue = (std::sin(time) * 2 + 1) / 2.0;
glClearColor(red, green, blue, 1.f);
glColor(GL_COLOR_BUFFER_BIT);
SDL_GL_SwapWindow(window);
// ...
But I have these errors:
C/C++: E:/_Projects/SDL3/sdl3-sample/src/main.cpp:38: error: undefined reference to 'glClearColor'
C/C++: E:/_Projects/SDL3/sdl3-sample/src/main.cpp:39: error: undefined reference to 'glClear'
All examples that I found in SDL for Android included this:
#include <SDL_opengles2.h>
But I don’t understand why it doesn’t work in this example. Maybe I should add something in config files. Please, help me to run this example.
main.cpp
#include <iostream>
#include <SDL.h>
#include <cmath>
#include <SDL_opengles2.h>
#if defined(WINAPI_FAMILY) && (WINAPI_FAMILY == WINAPI_FAMILY_APP)
//On UWP, we need to not have SDL_main otherwise we'll get a linker error
#define SDL_MAIN_HANDLED
#endif
#include <SDL_main.h>
#if __EMSCRIPTEN__
#include <emscripten.h>
#endif
void SDL_Fail(){
SDL_LogError(SDL_LOG_CATEGORY_CUSTOM, "Error %s", SDL_GetError());
exit(1);
}
static bool app_quit = false;
SDL_Window* window = nullptr;
void main_loop() {
// Get events. If you are making a game, you probably want SDL_PollEvent instead of SDL_WaitEvent.
// you cannot use WaitEvent on Emscripten, because you cannot block the main thread there.
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_EVENT_QUIT)
app_quit = true;
break;
}
// draw a color
auto time = SDL_GetTicks() / 1000.f;
auto red = (std::sin(time) + 1) / 2.0;
auto green = (std::sin(time / 2) + 1) / 2.0;
auto blue = (std::sin(time) * 2 + 1) / 2.0;
glClearColor(red, green, blue, 1.f);
glClear(GL_COLOR_BUFFER_BIT);
SDL_GL_SwapWindow(window);
}
// Note: your main function __must__ take this form, otherwise on nonstandard platforms (iOS, etc), your app will not launch.
int main(int argc, char* argv[]){
// init the library, here we make a window so we only need the Video capabilities.
if (SDL_Init(SDL_INIT_VIDEO)){
SDL_Fail();
}
// create a window
SDL_Window* window = SDL_CreateWindow("Window", 352, 430, SDL_WINDOW_OPENGL);
if (!window){
SDL_Fail();
}
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GLContext glContext = SDL_GL_CreateContext(window);
if (!glContext)
{
SDL_Fail();
}
// print some information about the window
SDL_ShowWindow(window);
{
int width, height, bbwidth, bbheight;
SDL_GetWindowSize(window, &width, &height);
SDL_GetWindowSizeInPixels(window, &bbwidth, &bbheight);
SDL_Log("Window size: %ix%i", width, height);
SDL_Log("Backbuffer size: %ix%i", bbwidth, bbheight);
if (width != bbwidth){
SDL_Log("This is a highdpi environment.");
}
}
SDL_Log("Application started successfully!");
#if __EMSCRIPTEN__
// on Emscripten, we cannot have an infinite loop in main. Instead, we must
// tell emscripten to call our main loop.
emscripten_set_main_loop(main_loop, 0, 1);
#else
while (!app_quit) {
main_loop();
}
#endif
// cleanup everything at the end
#if !__EMSCRIPTEN__
// SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
SDL_Log("Application quit successfully!");
#endif
return 0;
}
Cross-ref: