testsoftwaretransparent.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
  3. This software is provided 'as-is', without any express or implied
  4. warranty. In no event will the authors be held liable for any damages
  5. arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it
  8. freely.
  9. */
  10. #include <SDL3/SDL.h>
  11. #include <SDL3/SDL_main.h>
  12. #include "glass.h"
  13. int main(int argc, char *argv[])
  14. {
  15. SDL_Window *window = NULL;
  16. SDL_Renderer *renderer = NULL;
  17. SDL_Texture *texture = NULL;
  18. SDL_WindowFlags flags;
  19. bool done = false;
  20. SDL_Event event;
  21. int return_code = 1;
  22. int windowWidth = 800;
  23. int windowHeight = 600;
  24. SDL_FRect destRect;
  25. destRect.x = 0;
  26. destRect.y = 0;
  27. destRect.w = 100;
  28. destRect.h = 100;
  29. SDL_FRect destRect2;
  30. destRect2.x = 700;
  31. destRect2.y = 0;
  32. destRect2.w = 100;
  33. destRect2.h = 100;
  34. SDL_FRect destRect3;
  35. destRect3.x = 0;
  36. destRect3.y = 500;
  37. destRect3.w = 100;
  38. destRect3.h = 100;
  39. SDL_FRect destRect4;
  40. destRect4.x = 700;
  41. destRect4.y = 500;
  42. destRect4.w = 100;
  43. destRect4.h = 100;
  44. SDL_FRect destRect5;
  45. destRect5.x = 350;
  46. destRect5.y = 250;
  47. destRect5.w = 100;
  48. destRect5.h = 100;
  49. /* Create the window hidden */
  50. flags = (SDL_WINDOW_HIDDEN | SDL_WINDOW_TRANSPARENT);
  51. /*flags |= SDL_WINDOW_BORDERLESS;*/
  52. window = SDL_CreateWindow("SDL Software Renderer Transparent Test", windowWidth, windowHeight, flags);
  53. if (!window) {
  54. SDL_Log("Couldn't create transparent window: %s", SDL_GetError());
  55. goto quit;
  56. }
  57. /* Create a software renderer and set the blend mode */
  58. renderer = SDL_CreateRenderer(window, SDL_SOFTWARE_RENDERER);
  59. if (!renderer) {
  60. SDL_Log("Couldn't create renderer: %s", SDL_GetError());
  61. goto quit;
  62. }
  63. if (!SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND)) {
  64. SDL_Log("Couldn't set renderer blend mode: %s\n", SDL_GetError());
  65. return false;
  66. }
  67. /* Create texture and set the blend mode */
  68. /* texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA32, SDL_TEXTUREACCESS_TARGET, windowWidth, windowHeight); */
  69. /* texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, windowWidth, windowHeight); */
  70. /* texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_BGRA32, SDL_TEXTUREACCESS_TARGET, windowWidth, windowHeight); */
  71. texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_BGRA8888, SDL_TEXTUREACCESS_TARGET, windowWidth, windowHeight);
  72. if (texture == NULL) {
  73. SDL_Log("Couldn't create texture: %s\n", SDL_GetError());
  74. return false;
  75. }
  76. if (!SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND_PREMULTIPLIED)) {
  77. SDL_Log("Couldn't set texture blend mode: %s\n", SDL_GetError());
  78. return false;
  79. }
  80. /* Show */
  81. SDL_ShowWindow(window);
  82. /* We're ready to go! */
  83. while (!done) {
  84. while (SDL_PollEvent(&event)) {
  85. switch (event.type) {
  86. case SDL_EVENT_KEY_DOWN:
  87. if (event.key.key == SDLK_ESCAPE) {
  88. done = true;
  89. }
  90. break;
  91. case SDL_EVENT_QUIT:
  92. done = true;
  93. break;
  94. default:
  95. break;
  96. }
  97. }
  98. /* Draw opaque red squares at the four corners of the form, and draw a red square with an alpha value of 180 in the center of the form */
  99. SDL_SetRenderTarget(renderer, texture);
  100. SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
  101. SDL_RenderClear(renderer);
  102. SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
  103. SDL_RenderFillRect(renderer, &destRect);
  104. SDL_RenderFillRect(renderer, &destRect2);
  105. SDL_RenderFillRect(renderer, &destRect3);
  106. SDL_RenderFillRect(renderer, &destRect4);
  107. SDL_SetRenderDrawColor(renderer, 255, 0, 0, 180);
  108. SDL_RenderFillRect(renderer, &destRect5);
  109. SDL_SetRenderTarget(renderer, NULL);
  110. SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
  111. SDL_RenderClear(renderer);
  112. SDL_RenderTexture(renderer, texture, NULL, NULL);
  113. /* Show everything on the screen and wait a bit */
  114. SDL_RenderPresent(renderer);
  115. SDL_Delay(100);
  116. }
  117. /* Success! */
  118. return_code = 0;
  119. quit:
  120. SDL_DestroyTexture(texture);
  121. SDL_DestroyRenderer(renderer);
  122. SDL_DestroyWindow(window);
  123. SDL_Quit();
  124. return return_code;
  125. }