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.
 
 
 
 
 

152 lines
4.0 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<X11/X.h>
  7. #include<X11/Xlib.h>
  8. #include<X11/extensions/Xrandr.h>
  9. #include<GL/gl.h>
  10. #include<GL/glx.h>
  11. #include<GL/glu.h>
  12. #include "shader.h"
  13. #define CANVAS_WIDTH 1920
  14. #define CANVAS_HEIGHT 1080
  15. // #define DEBUG true
  16. static unsigned char fbdata[4 * CANVAS_HEIGHT * CANVAS_WIDTH];
  17. void _start() {
  18. asm volatile("sub $8, %rsp\n");
  19. Display* dpy = XOpenDisplay(NULL);
  20. Window root = DefaultRootWindow(dpy);
  21. static GLint att[] = { GLX_RGBA, None };
  22. XVisualInfo* vi = glXChooseVisual(dpy, 0, att);
  23. //I really hate this and I wish this call was unneeded. it feels useless
  24. Colormap cmap = XCreateColormap(dpy, root, vi->visual, AllocNone);
  25. //hide cursor
  26. XColor xcolor;
  27. Pixmap csr= XCreatePixmap(dpy,root,1,1,1);
  28. Cursor cursor= XCreatePixmapCursor(dpy,csr,csr,&xcolor,&xcolor,1,1);
  29. //this enables things like events, fullscreen, and sets the invisible cursor
  30. static XSetWindowAttributes swa = { .override_redirect = 1, .event_mask = ExposureMask | KeyPressMask };
  31. swa.colormap = cmap;
  32. swa.cursor = cursor;
  33. Window win = XCreateWindow(dpy, root, 0, 0, CANVAS_WIDTH, CANVAS_HEIGHT, 0, vi->depth, InputOutput, vi->visual, CWColormap | CWEventMask | CWOverrideRedirect | CWCursor, &swa);
  34. //this actually opens the window
  35. XMapWindow(dpy, win);
  36. //now we can do opengl calls!!!!
  37. GLXContext glc = glXCreateContext(dpy, vi, NULL, 1);
  38. #ifdef DEBUG
  39. if (glc == NULL) {
  40. return;
  41. }
  42. #endif
  43. glXMakeCurrent(dpy, win, glc);
  44. glClear(GL_COLOR_BUFFER_BIT);
  45. //oh yeah grab the keyboard
  46. XGrabKeyboard(dpy, win, true, GrabModeAsync, GrabModeAsync, CurrentTime);
  47. //create a floating point backing texture for a framebuffer
  48. GLuint textureA;
  49. glEnable(GL_TEXTURE_2D);
  50. glGenTextures(1, &textureA);
  51. glBindTexture(GL_TEXTURE_2D, textureA);
  52. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  53. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  54. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, CANVAS_WIDTH, CANVAS_HEIGHT, 0, GL_BGRA, GL_UNSIGNED_BYTE, fbdata);
  55. //create a framebuffer we can render everything to
  56. GLuint fboA;
  57. glGenFramebuffers(1, &fboA);
  58. glBindFramebuffer(GL_FRAMEBUFFER, fboA);
  59. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
  60. GL_TEXTURE_2D, textureA, 0);
  61. // compile shader
  62. GLuint f = glCreateShader(GL_FRAGMENT_SHADER);
  63. glShaderSource(f, 1, &shader_frag, NULL);
  64. glCompileShader(f);
  65. #ifdef DEBUG
  66. GLint isCompiled = 0;
  67. glGetShaderiv(f, GL_COMPILE_STATUS, &isCompiled);
  68. if(isCompiled == GL_FALSE) {
  69. GLint maxLength = 0;
  70. glGetShaderiv(f, GL_INFO_LOG_LENGTH, &maxLength);
  71. char* error = malloc(maxLength);
  72. glGetShaderInfoLog(f, maxLength, &maxLength, error);
  73. printf("%s\n", error);
  74. exit(-10);
  75. }
  76. #endif
  77. // link shader
  78. GLuint p = glCreateProgram();
  79. glAttachShader(p,f);
  80. glLinkProgram(p);
  81. #ifdef DEBUG
  82. GLint isLinked = 0;
  83. glGetProgramiv(p, GL_LINK_STATUS, (int *)&isLinked);
  84. if (isLinked == GL_FALSE) {
  85. GLint maxLength = 0;
  86. glGetProgramiv(p, GL_INFO_LOG_LENGTH, &maxLength);
  87. char* error = malloc(maxLength);
  88. glGetProgramInfoLog(p, maxLength, &maxLength,error);
  89. printf("%s\n", error);
  90. exit(-10);
  91. }
  92. #endif
  93. glUseProgram(p);
  94. //switch to using our framebuffer
  95. glBindFramebuffer(GL_FRAMEBUFFER, fboA);
  96. // glFinish();
  97. glRecti(-1,-1,1,1);
  98. //blit our framebuffer to the screen
  99. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
  100. glBindFramebuffer(GL_READ_FRAMEBUFFER, fboA);
  101. glBlitFramebuffer(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT, 0, 0, CANVAS_WIDTH, CANVAS_HEIGHT, GL_COLOR_BUFFER_BIT, GL_NEAREST);
  102. static XEvent xev;
  103. while(1) {
  104. XNextEvent(dpy, &xev);
  105. //wait for escape key, then exit without glib :3
  106. if(xev.type == KeyPress && xev.xkey.keycode == 0x09) {
  107. //blackle mori no likey AT&T
  108. asm volatile(".intel_syntax noprefix");
  109. asm volatile("push 60");
  110. asm volatile("pop rax");
  111. asm volatile("xor edi, edi");
  112. asm volatile("syscall");
  113. asm volatile(".att_syntax prefix");
  114. __builtin_unreachable();
  115. }
  116. }
  117. }