Drop support for SDL1, which is no longer maintained.
[movit] / gtest_sdl_main.cpp
1 #define GTEST_HAS_EXCEPTIONS 0
2
3 #include <SDL2/SDL.h>
4 #include <SDL2/SDL_error.h>
5 #include <SDL2/SDL_video.h>
6 #ifdef HAVE_BENCHMARK
7 #include <benchmark/benchmark.h>
8 #endif
9 #include <stdio.h>
10 #include <stdlib.h>
11
12 #include "gtest/gtest.h"
13
14 int main(int argc, char **argv) {
15         // Set up an OpenGL context using SDL.
16         if (SDL_Init(SDL_INIT_VIDEO) == -1) {
17                 fprintf(stderr, "SDL_Init failed: %s\n", SDL_GetError());
18                 exit(1);
19         }
20         SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0);
21         SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 0);
22         SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
23
24         // You can uncomment this if you want to try a core context.
25         // For Mesa, you can get the same effect by doing
26         //
27         //   export MESA_GL_VERSION_OVERRIDE=3.1FC
28         //
29         // before running tests.
30 //      SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
31 //      SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
32 //      SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
33
34         // See also init.cpp for how to enable debugging.
35 //      SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG);
36
37         SDL_Window *window = SDL_CreateWindow("OpenGL window for unit test",
38                 SDL_WINDOWPOS_UNDEFINED,
39                 SDL_WINDOWPOS_UNDEFINED,
40                 32, 32,
41                 SDL_WINDOW_OPENGL);
42         SDL_GLContext context = SDL_GL_CreateContext(window);
43         assert(context != nullptr);
44
45         int err;
46         if (argc >= 2 && strcmp(argv[1], "--benchmark") == 0) {
47 #ifdef HAVE_BENCHMARK
48                 --argc;
49                 ::benchmark::Initialize(&argc, argv + 1);
50                 if (::benchmark::ReportUnrecognizedArguments(argc, argv)) return 1;
51                 ::benchmark::RunSpecifiedBenchmarks();
52                 err = 0;
53 #else
54                 fprintf(stderr, "No support for microbenchmarks compiled in.\n");
55                 err = 1;
56 #endif
57         } else {
58                 testing::InitGoogleTest(&argc, argv);
59                 err = RUN_ALL_TESTS();
60         }
61         SDL_Quit();
62         return err;
63 }