test-random.c 1.8 KB

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