test-random.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include <stdlib.h>
  2. #include <obs.h>
  3. struct random_tex {
  4. texture_t texture;
  5. };
  6. static const char *random_getname(const char *locale)
  7. {
  8. UNUSED_PARAMETER(locale);
  9. return "20x20 Random Pixel Texture Source (Test)";
  10. }
  11. static void random_destroy(void *data)
  12. {
  13. struct random_tex *rt = data;
  14. if (rt) {
  15. gs_entercontext(obs_graphics());
  16. texture_destroy(rt->texture);
  17. bfree(rt);
  18. gs_leavecontext();
  19. }
  20. }
  21. static void *random_create(obs_data_t settings, obs_source_t source)
  22. {
  23. struct random_tex *rt = bzalloc(sizeof(struct random_tex));
  24. uint32_t *pixels = bmalloc(20*20*4);
  25. size_t x, y;
  26. for (y = 0; y < 20; y++) {
  27. for (x = 0; x < 20; x++) {
  28. uint32_t pixel = 0xFF000000;
  29. pixel |= (rand()%256);
  30. pixel |= (rand()%256) << 8;
  31. pixel |= (rand()%256) << 16;
  32. //pixel |= 0xFFFFFFFF;
  33. pixels[y*20 + x] = pixel;
  34. }
  35. }
  36. gs_entercontext(obs_graphics());
  37. rt->texture = gs_create_texture(20, 20, GS_RGBA, 1,
  38. (const void**)&pixels, 0);
  39. bfree(pixels);
  40. if (!rt->texture) {
  41. random_destroy(rt);
  42. return NULL;
  43. }
  44. gs_leavecontext();
  45. UNUSED_PARAMETER(settings);
  46. UNUSED_PARAMETER(source);
  47. return rt;
  48. }
  49. static void random_video_render(void *data, effect_t effect)
  50. {
  51. struct random_tex *rt = data;
  52. eparam_t image = effect_getparambyname(effect, "image");
  53. effect_settexture(effect, image, rt->texture);
  54. gs_draw_sprite(rt->texture, 0, 0, 0);
  55. }
  56. static uint32_t random_getwidth(void *data)
  57. {
  58. struct random_tex *rt = data;
  59. return texture_getwidth(rt->texture);
  60. }
  61. static uint32_t random_getheight(void *data)
  62. {
  63. struct random_tex *rt = data;
  64. return texture_getheight(rt->texture);
  65. }
  66. struct obs_source_info test_random = {
  67. .id = "random",
  68. .type = OBS_SOURCE_TYPE_INPUT,
  69. .output_flags = OBS_SOURCE_VIDEO,
  70. .getname = random_getname,
  71. .create = random_create,
  72. .destroy = random_destroy,
  73. .video_render = random_video_render,
  74. .getwidth = random_getwidth,
  75. .getheight = random_getheight
  76. };