test-random.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #include <stdlib.h>
  2. #include "test-random.h"
  3. const char *random_getname(const char *locale)
  4. {
  5. return "20x20 Random Pixel Texture Source (Test)";
  6. }
  7. struct random_tex *random_create(const char *settings, obs_source_t source)
  8. {
  9. struct random_tex *rt = bmalloc(sizeof(struct random_tex));
  10. uint32_t *pixels = bmalloc(20*20*4);
  11. size_t x, y;
  12. memset(rt, 0, sizeof(struct random_tex));
  13. for (y = 0; y < 20; y++) {
  14. for (x = 0; x < 20; x++) {
  15. uint32_t pixel = 0xFF000000;
  16. pixel |= (rand()%256);
  17. pixel |= (rand()%256) << 8;
  18. pixel |= (rand()%256) << 16;
  19. //pixel |= 0xFFFFFFFF;
  20. pixels[y*20 + x] = pixel;
  21. }
  22. }
  23. gs_entercontext(obs_graphics());
  24. rt->texture = gs_create_texture(20, 20, GS_RGBA, 1,
  25. (const void**)&pixels, 0);
  26. bfree(pixels);
  27. if (!rt->texture) {
  28. random_destroy(rt);
  29. return NULL;
  30. }
  31. gs_leavecontext();
  32. return rt;
  33. }
  34. void random_destroy(struct random_tex *rt)
  35. {
  36. if (rt) {
  37. gs_entercontext(obs_graphics());
  38. texture_destroy(rt->texture);
  39. bfree(rt);
  40. gs_leavecontext();
  41. }
  42. }
  43. uint32_t random_get_output_flags(struct random_tex *rt)
  44. {
  45. return SOURCE_VIDEO | SOURCE_DEFAULT_EFFECT;
  46. }
  47. void random_video_render(struct random_tex *rt, obs_source_t filter_target)
  48. {
  49. effect_t effect = gs_geteffect();
  50. eparam_t diffuse = effect_getparambyname(effect, "diffuse");
  51. effect_settexture(effect, diffuse, rt->texture);
  52. gs_draw_sprite(rt->texture, 0, 0, 0);
  53. }
  54. uint32_t random_getwidth(struct random_tex *rt)
  55. {
  56. return texture_getwidth(rt->texture);
  57. }
  58. uint32_t random_getheight(struct random_tex *rt)
  59. {
  60. return texture_getheight(rt->texture);
  61. }