Advertisement
Guest User

Untitled

a guest
Feb 9th, 2022
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.17 KB | None | 0 0
  1. // https://lazyfoo.net/tutorials/SDL/01_hello_SDL/index2.php
  2. // in zig, to prove just how drop in c compatibility really is:
  3.  
  4. // You can also add C files to your builds without a problem, so this
  5. // should be useful when transitioning large code bases to zig. In
  6. // addition to this, you can also write zig to export a C abi
  7. // (but I'm not sure if there are any automatic header generation
  8. // tools).
  9.  
  10. const std = @import("std");
  11. const print = std.debug.print;
  12. // See @cDefine @cUndef and the zig translate-c tool if you need more
  13. // control over c includes
  14. const s = @cImport({
  15.     @cInclude("SDL2/SDL.h");
  16.     // You can have as many includes as you want...
  17.     // In this case I'm not really going to use stdio.h, but whatever
  18.     @cInclude("stdio.h");
  19. });
  20.  
  21.  
  22. const SCREEN_WIDTH = 640;
  23. const SCREEN_HEIGHT = 480;
  24.  
  25. pub fn main() u8 {
  26.     // Window is an opaque type, so we use a normal pointer
  27.     var window: *s.SDL_Window = undefined;
  28.     // [*c] is a c pointer, to help with c interop
  29.     var surface: [*c]s.SDL_Surface = 0;
  30.  
  31.     // Zig is very annoying with unused variables, so if there's a
  32.     // var you haven't gotten to using yet, just do _ = your_var; and
  33.     // that'll shut it up (it's a noop):
  34.     _ = window;
  35.  
  36.     if (s.SDL_Init(s.SDL_INIT_VIDEO) < 0) {
  37.         print("SDL could not initialize! SDL_Error: {s}\n",
  38.               .{s.SDL_GetError()});
  39.         return 1;
  40.     }
  41.     // There is also a errdefer which only fires if the function
  42.     // returns an error
  43.     defer s.SDL_Quit();
  44.  
  45.     window = s.SDL_CreateWindow("SDL Tutorial",
  46.                                 s.SDL_WINDOWPOS_UNDEFINED,
  47.                                 s.SDL_WINDOWPOS_UNDEFINED,
  48.                                 SCREEN_WIDTH,
  49.                                 SCREEN_HEIGHT,
  50.                                 s.SDL_WINDOW_SHOWN) orelse {
  51.         // orelse fires if the result is null
  52.         print("Window could not be created! SDL_Error: {s}\n",
  53.               .{s.SDL_GetError()});
  54.         return 2;
  55.     };
  56.     defer s.SDL_DestroyWindow(window);
  57.  
  58.     surface = s.SDL_GetWindowSurface(window);
  59.     // _ = * is needed to ignore return values as well
  60.     _ = s.SDL_FillRect(surface, null,
  61.                        s.SDL_MapRGB(surface.*.format, 0xF0, 0xF0, 0xF0));
  62.     _ = s.SDL_UpdateWindowSurface(window);
  63.     s.SDL_Delay(2000);
  64.  
  65.     return 0;
  66. }
  67.  
  68.  
  69. // And the build script looks like this:
  70. ///////////////////////////////////////////////////////////////////////
  71. // ...                                                               //
  72. //     const exe = b.addExecutable("zigcheatsheet", "src/main.zig"); //
  73. //     exe.linkLibC();                                               //
  74. //     exe.addIncludeDir("/usr/include");                            //
  75. //     exe.linkSystemLibrary("SDL2");                                //
  76. //     exe.setTarget(target);                                        //
  77. //     exe.setBuildMode(mode);                                       //
  78. //     exe.install();                                                //
  79. // ...                                                               //
  80. ///////////////////////////////////////////////////////////////////////
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement