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.

153 lines
3.6 KiB

  1. #define GL_GLEXT_PROTOTYPES why
  2. #include<stdio.h>
  3. #include<stdbool.h>
  4. #include<stdlib.h>
  5. #include<stdint.h>
  6. #include <glib.h>
  7. #include <gtk/gtk.h>
  8. #include <gdk/gdkkeysyms.h>
  9. #include <GL/gl.h>
  10. #include <GL/glx.h>
  11. #include <GL/glu.h>
  12. #include <GL/glext.h>
  13. #include "shader.h"
  14. static char* vshader = "#version 450\nvec2 y=vec2(1.,-1);\nvec4 x[4]={y.yyxx,y.xyxx,y.yxxx,y.xxxx};void main(){gl_Position=x[gl_VertexID];}";
  15. #define CANVAS_WIDTH 1920
  16. #define CANVAS_HEIGHT 1080
  17. // #define DEBUG
  18. GLuint vao;
  19. GLuint p;
  20. GTimer* gtimer;
  21. static gboolean
  22. on_render (GtkGLArea *glarea, GdkGLContext *context)
  23. {
  24. float itime = g_timer_elapsed(gtimer, NULL);
  25. if (itime > 10.0) gtk_main_quit();
  26. glUseProgram(p);
  27. glUniform1f(0, itime);
  28. glBindVertexArray(vao);
  29. glVertexAttrib1f(0, 0);
  30. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  31. return TRUE;
  32. }
  33. static void on_realize(GtkGLArea *glarea)
  34. {
  35. gtk_gl_area_make_current(glarea);
  36. // compile shader
  37. GLuint f = glCreateShader(GL_FRAGMENT_SHADER);
  38. glShaderSource(f, 1, &shader_frag, NULL);
  39. glCompileShader(f);
  40. #ifdef DEBUG
  41. GLint isCompiled = 0;
  42. glGetShaderiv(f, GL_COMPILE_STATUS, &isCompiled);
  43. if(isCompiled == GL_FALSE) {
  44. GLint maxLength = 0;
  45. glGetShaderiv(f, GL_INFO_LOG_LENGTH, &maxLength);
  46. char* error = malloc(maxLength);
  47. glGetShaderInfoLog(f, maxLength, &maxLength, error);
  48. printf("%s\n", error);
  49. exit(-10);
  50. }
  51. #endif
  52. GLuint v = glCreateShader(GL_VERTEX_SHADER);
  53. glShaderSource(v, 1, &vshader, NULL);
  54. glCompileShader(v);
  55. #ifdef DEBUG
  56. GLint isCompiled2 = 0;
  57. glGetShaderiv(v, GL_COMPILE_STATUS, &isCompiled2);
  58. if(isCompiled2 == GL_FALSE) {
  59. GLint maxLength = 0;
  60. glGetShaderiv(v, GL_INFO_LOG_LENGTH, &maxLength);
  61. char* error = malloc(maxLength);
  62. glGetShaderInfoLog(v, maxLength, &maxLength, error);
  63. printf("%s\n", error);
  64. exit(-10);
  65. }
  66. #endif
  67. // link shader
  68. p = glCreateProgram();
  69. glAttachShader(p,v);
  70. glAttachShader(p,f);
  71. glLinkProgram(p);
  72. #ifdef DEBUG
  73. GLint isLinked = 0;
  74. glGetProgramiv(p, GL_LINK_STATUS, (int *)&isLinked);
  75. if (isLinked == GL_FALSE) {
  76. GLint maxLength = 0;
  77. glGetProgramiv(p, GL_INFO_LOG_LENGTH, &maxLength);
  78. char* error = malloc(maxLength);
  79. glGetProgramInfoLog(p, maxLength, &maxLength,error);
  80. printf("%s\n", error);
  81. exit(-10);
  82. }
  83. #endif
  84. glGenVertexArrays(1, &vao);
  85. GdkGLContext *context = gtk_gl_area_get_context(glarea);
  86. GdkWindow *glwindow = gdk_gl_context_get_window(context);
  87. GdkFrameClock *frame_clock = gdk_window_get_frame_clock(glwindow);
  88. // Connect update signal:
  89. g_signal_connect_swapped(frame_clock, "update", G_CALLBACK(gtk_gl_area_queue_render), glarea);
  90. // Start updating:
  91. gdk_frame_clock_begin_updating(frame_clock);
  92. }
  93. void _start() {
  94. asm volatile("sub $8, %rsp\n");
  95. typedef void (*voidWithOneParam)(int*);
  96. voidWithOneParam gtk_init_one_param = (voidWithOneParam)gtk_init;
  97. (*gtk_init_one_param)(NULL);
  98. gtimer = g_timer_new();
  99. GtkWidget *win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  100. GtkWidget *glarea = gtk_gl_area_new();
  101. gtk_container_add(GTK_CONTAINER(win), glarea);
  102. g_signal_connect(win, "destroy", gtk_main_quit, NULL);
  103. g_signal_connect(glarea, "realize", G_CALLBACK(on_realize), NULL);
  104. g_signal_connect(glarea, "render", G_CALLBACK(on_render), NULL);
  105. gtk_widget_show_all (win);
  106. gtk_window_fullscreen((GtkWindow*)win);
  107. GdkWindow* window = gtk_widget_get_window(win);
  108. GdkCursor* Cursor = gdk_cursor_new(GDK_BLANK_CURSOR);
  109. gdk_window_set_cursor(window, Cursor);
  110. gtk_main();
  111. asm volatile(".intel_syntax noprefix");
  112. asm volatile("push 231"); //exit_group
  113. asm volatile("pop rax");
  114. // asm volatile("xor edi, edi");
  115. asm volatile("syscall");
  116. asm volatile(".att_syntax prefix");
  117. __builtin_unreachable();
  118. // return 0;
  119. }