test-random.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include <stdlib.h>
  2. #include "test-random.h"
  3. struct random_tex *random_create(const char *settings, obs_source_t source)
  4. {
  5. struct random_tex *rt = bmalloc(sizeof(struct random_tex));
  6. uint32_t *pixels = bmalloc(20*20*4);
  7. size_t x, y;
  8. memset(rt, 0, sizeof(struct random_tex));
  9. for (y = 0; y < 20; y++) {
  10. for (x = 0; x < 20; x++) {
  11. uint32_t pixel = 0xFF000000;
  12. pixel |= (rand()%256);
  13. pixel |= (rand()%256) << 8;
  14. pixel |= (rand()%256) << 16;
  15. //pixel |= 0xFFFFFFFF;
  16. pixels[y*20 + x] = pixel;
  17. }
  18. }
  19. gs_entercontext(obs_graphics());
  20. rt->texture = gs_create_texture(20, 20, GS_RGBA, 1,
  21. (const void**)&pixels, 0);
  22. bfree(pixels);
  23. if (!rt->texture) {
  24. random_destroy(rt);
  25. return NULL;
  26. }
  27. rt->whatever = gs_create_effect_from_file("draw.effect", NULL);
  28. if (!rt->whatever) {
  29. random_destroy(rt);
  30. return NULL;
  31. }
  32. gs_leavecontext();
  33. return rt;
  34. }
  35. void random_destroy(struct random_tex *rt)
  36. {
  37. if (rt) {
  38. gs_entercontext(obs_graphics());
  39. effect_destroy(rt->whatever);
  40. texture_destroy(rt->texture);
  41. bfree(rt);
  42. gs_leavecontext();
  43. }
  44. }
  45. uint32_t random_get_output_flags(struct random_tex *rt)
  46. {
  47. return SOURCE_VIDEO;
  48. }
  49. void random_video_render(struct random_tex *rt, obs_source_t filter_target)
  50. {
  51. technique_t tech = effect_gettechnique(rt->whatever, "Default");
  52. effect_settexture(rt->whatever, effect_getparambyidx(rt->whatever, 1),
  53. rt->texture);
  54. technique_begin(tech);
  55. technique_beginpass(tech, 0);
  56. gs_draw_sprite(rt->texture);
  57. technique_endpass(tech);
  58. technique_end(tech);
  59. }
  60. int random_getwidth(struct random_tex *rt)
  61. {
  62. return texture_getwidth(rt->texture);
  63. }
  64. int random_getheight(struct random_tex *rt)
  65. {
  66. return texture_getheight(rt->texture);
  67. }