@kmamal/sdl 0.11.2 → 0.11.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.11.2",
2
+ "version": "0.11.3",
3
3
  "name": "@kmamal/sdl",
4
4
  "description": "SDL bindings for Node.js",
5
5
  "keywords": [
package/scripts/build.mjs CHANGED
@@ -55,3 +55,8 @@ await Promise.all([
55
55
  }))
56
56
  })(),
57
57
  ])
58
+
59
+ // Strip binaries on linux
60
+ if (C.platform === 'linux') {
61
+ execSync(`strip -s ${Path.join(C.dir.dist, 'sdl.node')}`)
62
+ }
@@ -3,7 +3,17 @@
3
3
  #include <SDL_syswm.h>
4
4
  #include <string>
5
5
  #include <sstream>
6
+ #include <map>
6
7
 
8
+ struct CachedTexture {
9
+ SDL_Texture *texture;
10
+ int width;
11
+ int height;
12
+ unsigned int format;
13
+ SDL_ScaleMode scaling;
14
+ };
15
+
16
+ std::map<SDL_Window*, CachedTexture> cachedTextures;
7
17
 
8
18
  #if defined(__LINUX__)
9
19
  #define NativeWindowHandle Window
@@ -31,7 +41,6 @@
31
41
  #define GPU_WINDOW_FLAG SDL_WINDOW_METAL
32
42
  #endif
33
43
 
34
-
35
44
  void
36
45
  updateRenderer(
37
46
  Napi::Env &env,
@@ -42,6 +51,12 @@ updateRenderer(
42
51
  int window_id = SDL_GetWindowID(window);
43
52
  // Not likely to fail.
44
53
 
54
+ CachedTexture &cached = cachedTextures[window];
55
+ if (cached.texture != nullptr) {
56
+ SDL_DestroyTexture(cached.texture);
57
+ cached.texture = nullptr;
58
+ }
59
+
45
60
  SDL_Renderer *old_renderer = SDL_GetRenderer(window);
46
61
  if (old_renderer != nullptr) {
47
62
  SDL_DestroyRenderer(old_renderer);
@@ -190,7 +205,8 @@ window::create (const Napi::CallbackInfo &info)
190
205
  #elif defined(__MACOSX__)
191
206
  *native_gl = getCocoaGlView(sys_wm_info.info.cocoa.window);
192
207
  #endif
193
- } else if (is_webgpu) {
208
+ }
209
+ else if (is_webgpu) {
194
210
  int native_gpu_size = sizeof(GPU_NativeData);
195
211
  Napi::Buffer<char> native_gpu_buffer = Napi::Buffer<char>::New(env, native_gpu_size);
196
212
  GPU_NativeData *native_gpu = (GPU_NativeData *) native_gpu_buffer.Data();
@@ -207,6 +223,7 @@ window::create (const Napi::CallbackInfo &info)
207
223
  #endif
208
224
  }
209
225
  else {
226
+ cachedTextures[window] = {};
210
227
  updateRenderer(env, window, &is_accelerated, &is_vsync);
211
228
  }
212
229
 
@@ -536,8 +553,8 @@ window::render (const Napi::CallbackInfo &info)
536
553
  Napi::Env env = info.Env();
537
554
 
538
555
  int window_id = info[0].As<Napi::Number>().Int32Value();
539
- int w = info[1].As<Napi::Number>().Int32Value();
540
- int h = info[2].As<Napi::Number>().Int32Value();
556
+ int width = info[1].As<Napi::Number>().Int32Value();
557
+ int height = info[2].As<Napi::Number>().Int32Value();
541
558
  int stride = info[3].As<Napi::Number>().Int32Value();
542
559
  unsigned int format = info[4].As<Napi::Number>().Int32Value();
543
560
  void *pixels = info[5].As<Napi::Buffer<char>>().Data();
@@ -569,42 +586,43 @@ window::render (const Napi::CallbackInfo &info)
569
586
  throw Napi::Error::New(env, message.str());
570
587
  }
571
588
 
572
- SDL_Surface *surface = SDL_CreateRGBSurfaceWithFormatFrom(pixels, w, h, SDL_BITSPERPIXEL(format), stride, format);
573
- if (surface == nullptr) {
574
- std::ostringstream message;
575
- message << "SDL_CreateRGBSurfaceWithFormatFrom(" << window_id << ", " << w << ", " << h << ", " << stride << ", " << format << ") error: " << SDL_GetError();
576
- SDL_ClearError();
577
- throw Napi::Error::New(env, message.str());
578
- }
589
+ CachedTexture &cached = cachedTextures[window];
590
+ SDL_Texture *texture = cached.texture;
579
591
 
580
- if (SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_NONE) < 0) {
581
- std::ostringstream message;
582
- message << "SDL_SetSurfaceBlendMode(" << window_id << ") error: " << SDL_GetError();
583
- SDL_ClearError();
584
- throw Napi::Error::New(env, message.str());
585
- }
592
+ if (texture == nullptr
593
+ || cached.width != width
594
+ || cached.height != height
595
+ || cached.format != format
596
+ || cached.scaling != scaling
597
+ ) {
598
+ if (texture != nullptr) { SDL_DestroyTexture(texture); }
586
599
 
587
- SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
588
- if (texture == nullptr) {
589
- SDL_FreeSurface(surface);
600
+ texture = SDL_CreateTexture(renderer, format, SDL_TEXTUREACCESS_STREAMING, width, height);
590
601
 
591
- std::ostringstream message;
592
- message << "SDL_CreateTextureFromSurface(" << window_id << ") error: " << SDL_GetError();
593
- SDL_ClearError();
594
- throw Napi::Error::New(env, message.str());
595
- }
602
+ if (SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_NONE) < 0) {
603
+ std::ostringstream message;
604
+ message << "SDL_SetTextureBlendMode(" << window_id << ") error: " << SDL_GetError();
605
+ SDL_ClearError();
606
+ throw Napi::Error::New(env, message.str());
607
+ }
596
608
 
597
- if(SDL_SetTextureScaleMode(texture, scaling) < 0) {
598
- std::ostringstream message;
599
- message << "SDL_SetTextureScaleMode(" << window_id << ", " << scaling << ") error: " << SDL_GetError();
600
- SDL_ClearError();
601
- throw Napi::Error::New(env, message.str());
609
+ if(SDL_SetTextureScaleMode(texture, scaling) < 0) {
610
+ std::ostringstream message;
611
+ message << "SDL_SetTextureScaleMode(" << window_id << ", " << scaling << ") error: " << SDL_GetError();
612
+ SDL_ClearError();
613
+ throw Napi::Error::New(env, message.str());
614
+ }
615
+
616
+ cached.texture = texture;
617
+ cached.width = width;
618
+ cached.height = height;
619
+ cached.format = format;
620
+ cached.scaling = scaling;
602
621
  }
603
622
 
604
- if (SDL_RenderClear(renderer) < 0) {
605
- SDL_DestroyTexture(texture);
606
- SDL_FreeSurface(surface);
623
+ SDL_UpdateTexture(texture, nullptr, pixels, stride);
607
624
 
625
+ if (SDL_RenderClear(renderer) < 0) {
608
626
  std::ostringstream message;
609
627
  message << "SDL_RenderClear(" << window_id << ") error: " << SDL_GetError();
610
628
  SDL_ClearError();
@@ -612,9 +630,6 @@ window::render (const Napi::CallbackInfo &info)
612
630
  }
613
631
 
614
632
  if (SDL_RenderCopy(renderer, texture, nullptr, hasDstRect ? &rect : nullptr) < 0) {
615
- SDL_DestroyTexture(texture);
616
- SDL_FreeSurface(surface);
617
-
618
633
  std::ostringstream message;
619
634
  message << "SDL_RenderCopy(" << window_id << ") error: " << SDL_GetError();
620
635
  SDL_ClearError();
@@ -623,8 +638,6 @@ window::render (const Napi::CallbackInfo &info)
623
638
 
624
639
  SDL_RenderPresent(renderer);
625
640
 
626
- SDL_DestroyTexture(texture);
627
- SDL_FreeSurface(surface);
628
641
  return env.Undefined();
629
642
  }
630
643
 
@@ -712,7 +725,13 @@ window::destroy (const Napi::CallbackInfo &info)
712
725
  }
713
726
 
714
727
  SDL_Renderer *renderer = SDL_GetRenderer(window);
715
- if (renderer != nullptr) { SDL_DestroyRenderer(renderer); }
728
+ if (renderer != nullptr) {
729
+ SDL_DestroyRenderer(renderer);
730
+
731
+ CachedTexture &cached = cachedTextures[window];
732
+ if (cached.texture != nullptr) { SDL_DestroyTexture(cached.texture); }
733
+ cachedTextures.erase(window);
734
+ }
716
735
 
717
736
  SDL_DestroyWindow(window);
718
737