Code examples for opening windows for various purposes in very few bytes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

113 lines
2.2 KiB

  1. #define GL_GLEXT_PROTOTYPES
  2. #include<stdio.h>
  3. #include<stdbool.h>
  4. #include<stdlib.h>
  5. #include<stdint.h>
  6. #include <SDL2/SDL.h>
  7. #include <GL/gl.h>
  8. #include <GL/glx.h>
  9. #include <GL/glu.h>
  10. #include <GL/glext.h>
  11. #include "shader.h"
  12. #define CANVAS_WIDTH 1920
  13. #define CANVAS_HEIGHT 1080
  14. // #define KEY_HANDLING
  15. // #define DEBUG
  16. static void on_render ()
  17. {
  18. glRecti(-1,-1,1,1);
  19. }
  20. static void on_realize()
  21. {
  22. // compile shader
  23. GLuint f = glCreateShader(GL_FRAGMENT_SHADER);
  24. glShaderSource(f, 1, &shader_frag, NULL);
  25. glCompileShader(f);
  26. #ifdef DEBUG
  27. GLint isCompiled = 0;
  28. glGetShaderiv(f, GL_COMPILE_STATUS, &isCompiled);
  29. if(isCompiled == GL_FALSE) {
  30. GLint maxLength = 0;
  31. glGetShaderiv(f, GL_INFO_LOG_LENGTH, &maxLength);
  32. char* error = malloc(maxLength);
  33. glGetShaderInfoLog(f, maxLength, &maxLength, error);
  34. printf("%s\n", error);
  35. exit(-10);
  36. }
  37. #endif
  38. // link shader
  39. GLuint p = glCreateProgram();
  40. glAttachShader(p,f);
  41. glLinkProgram(p);
  42. #ifdef DEBUG
  43. GLint isLinked = 0;
  44. glGetProgramiv(p, GL_LINK_STATUS, (int *)&isLinked);
  45. if (isLinked == GL_FALSE) {
  46. GLint maxLength = 0;
  47. glGetProgramiv(p, GL_INFO_LOG_LENGTH, &maxLength);
  48. char* error = malloc(maxLength);
  49. glGetProgramInfoLog(p, maxLength, &maxLength,error);
  50. printf("%s\n", error);
  51. exit(-10);
  52. }
  53. #endif
  54. glUseProgram(p);
  55. }
  56. void _start() {
  57. asm volatile("sub $8, %rsp\n");
  58. SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS);
  59. SDL_Window* mainwindow = SDL_CreateWindow(
  60. "",
  61. 0,
  62. 0,
  63. CANVAS_WIDTH,
  64. CANVAS_HEIGHT,
  65. SDL_WINDOW_OPENGL | SDL_WINDOW_FULLSCREEN
  66. );
  67. SDL_GL_CreateContext(mainwindow);
  68. SDL_ShowCursor(false);
  69. on_realize();
  70. while (true) {
  71. SDL_Event Event;
  72. while (SDL_PollEvent(&Event)) {
  73. if (Event.type == SDL_QUIT
  74. #if defined(KEY_HANDLING)
  75. || (Event.type == SDL_KEYDOWN && Event.key.keysym.sym == SDLK_ESCAPE)
  76. #endif
  77. ) {
  78. asm volatile(".intel_syntax noprefix");
  79. asm volatile("push 231"); //exit_group
  80. asm volatile("pop rax");
  81. asm volatile("xor edi, edi");
  82. asm volatile("syscall");
  83. asm volatile(".att_syntax prefix");
  84. __builtin_unreachable();
  85. }
  86. if (Event.type == SDL_WINDOWEVENT && Event.window.event == SDL_WINDOWEVENT_EXPOSED) {
  87. on_render();
  88. SDL_GL_SwapWindow(mainwindow);
  89. }
  90. }
  91. }
  92. __builtin_unreachable();
  93. }