{"id":88,"date":"2022-05-12T23:43:14","date_gmt":"2022-05-13T06:43:14","guid":{"rendered":"https:\/\/blogs.oregonstate.edu\/usageoptions\/?p=88"},"modified":"2022-05-12T23:43:14","modified_gmt":"2022-05-13T06:43:14","slug":"using-sdl-part-2","status":"publish","type":"post","link":"https:\/\/blogs.oregonstate.edu\/usageoptions\/2022\/05\/12\/using-sdl-part-2\/","title":{"rendered":"Using SDL Part 2"},"content":{"rendered":"\n<p style=\"font-size:1rem\">This is part 2 in a series on using SDL. See <a href=\"https:\/\/blogs.oregonstate.edu\/usageoptions\/2022\/05\/05\/using-sdl-part-1\/\">part 1<\/a> if you missed it. This post introduces more useful functions, structures, and constants in the <a href=\"https:\/\/wiki.libsdl.org\/APIByCategory\">SDL 2.0 API<\/a>. It covers basic details such as return values and parameters of functions and names and data types of members of structs. For more detailed info, visit the links in the references.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" style=\"font-size:1.75rem\">Rendering<\/h2>\n\n\n\n<p style=\"font-size:1rem\">Call the <code>SDL_CreateRenderer()<\/code> function to create a renderer in which you can render objects such as rectangles in a window.<sup><a href=\"#ref\">[1]<\/a><\/sup> The window should have already been initialized. See <a href=\"https:\/\/blogs.oregonstate.edu\/usageoptions\/2022\/05\/05\/using-sdl-part-1\/\">part 1<\/a> to review how to create an <code>SDL_Window<\/code> instance. <\/p>\n\n\n\n<pre class=\"wp-block-code has-light-gray-background-color has-background\" style=\"font-size:1rem\"><code>SDL_Renderer *SDL_CreateRenderer(SDL_Window *window,\nint index, Uint32 flags);\n\n<strong>Returns:<\/strong> if successful, a valid rendering context; on error, NULL<\/code><\/pre>\n\n\n\n<p style=\"font-size:1rem\"><code>window<\/code> is an <code>SDL_Window<\/code> instance where the renderer will be displayed.<br><code>index<\/code> is the index of the rendering driver to initialize, or -1 to initialize the first one supporting the requested flags.<br><code>flags<\/code> is 0, or one or more <code>SDL_RendererFlags<\/code> OR&#8217;d together. Possible values for <code>flags<\/code>:<\/p>\n\n\n\n<ul class=\"wp-block-list\" style=\"font-size:1rem\"><li>SDL_RENDERER_SOFTWARE: the renderer is a software fallback<\/li><li>SDL_RENDERER_ACCELERATED: the renderer uses hardware acceleration<\/li><li>SDL_RENDERER_PRESENTVSYNC: present is synchronized with the refresh rate<\/li><li>SDL_RENDERER_TARGETTEXTURE: the renderer supports rendering to texture<\/li><\/ul>\n\n\n\n<p style=\"font-size:1rem\">You will likely want to render to a texture, which is created with <code>SDL_CreateTexture()<\/code>.<sup><a href=\"#ref\">[2]<\/a><\/sup><\/p>\n\n\n\n<pre class=\"wp-block-code has-light-gray-background-color has-background\"><code>SDL_Texture *SDL_CreateTexture(SDL_Renderer *renderer,\n                                Uint32 format,\n                                int access, int w,\n                                int h);\n\n<strong>Returns:<\/strong> a pointer to the created texture or NULL if no rendering context was active, the format was unsupported, or the width or height were out of range<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\" style=\"font-size:1rem\"><li><code>renderer<\/code> is the rendering context.<\/li><li><code>format<\/code> is one of the enumerated values in <a href=\"https:\/\/wiki.libsdl.org\/SDL_PixelFormatEnum\">SDL_PixelFormatEnum<\/a>.<\/li><li><code>access<\/code> is one of the enumerated values in <a href=\"https:\/\/wiki.libsdl.org\/SDL_TextureAccess\">SDL_TextureAccess<\/a>, which are<ul><li><code>SDL_TEXTUREACCESS_STATIC<\/code>: changes rarely, not lockable<\/li><li><code>SDL_TEXTUREACCESS_STREAMING<\/code>: changes frequently, lockable<\/li><li><code>SDL_TEXTUREACCESS_TARGET<\/code>: can be used as a render target<\/li><\/ul><\/li><li><code>w<\/code> is the width of the texture in pixels.<\/li><li><code>h<\/code> is the height of the texture in pixels.<\/li><\/ul>\n\n\n\n<p style=\"font-size:1rem\">Use <code>SDL_SetRenderTarget()<\/code> is set a texture as the target of a renderer.<sup><a href=\"#ref\">[3]<\/a><\/sup><\/p>\n\n\n\n<pre class=\"wp-block-code has-light-gray-background-color has-background\"><code>int SDL_SetRenderTarget(SDL_Renderer *renderer,\n                        SDL_Texture *texture);\n\n<strong>Returns:<\/strong> 0 on success or a negative error code on failure<\/code><\/pre>\n\n\n\n<p style=\"font-size:1rem\"><code>renderer<\/code> is the rendering context.<br><code>texture<\/code> is the targeted texture, which must be created with the <code>SDL_TEXTUREACCESS_TARGET<\/code> flag, or NULL to render to the window instead of a texture.<\/p>\n\n\n\n<p style=\"font-size:1rem\">To set the color used when rendering, use <code>SDL_SetRenderDrawColor()<\/code>.<sup><a href=\"#ref\">[4]<\/a><\/sup><\/p>\n\n\n\n<pre class=\"wp-block-code has-light-gray-background-color has-background\"><code>int SDL_SetRenderDrawColor(SDL_Renderer *renderer,\n                   Uint8 r, Uint8 g, Uint8 b,\n                   Uint8 a);\n\n<strong>Returns:<\/strong> 0 on success or a negative error code on failure<\/code><\/pre>\n\n\n\n<p style=\"font-size:1rem\"><code>renderer<\/code> is the rendering context.<br><code>r<\/code>, <code>g<\/code>, and <code>b<\/code> are the red, green, and blues value used to draw on the rendering target, respectively. These should be a value between 0 and 255, inclusive.<br><code>a<\/code> is the alpha value (opacity) used to draw on the rendering target. This should be a value between 0 and 255, inclusive. Use <code>SDL_ALPHA_OPAQUE<\/code> or 255 to specify opaque. If you want to have a transparent object, use <code><a href=\"https:\/\/wiki.libsdl.org\/SDL_SetRenderDrawBlendMode\">SDL_SetRenderDrawBlendMode()<\/a><\/code>.<\/p>\n\n\n\n<p style=\"font-size:1rem\">Use <code>SDL_RenderClear()<\/code><sup><a href=\"#ref\">[5]<\/a><\/sup> to color the entire rendering target with the color previously set with <code>SDL_SetRenderDrawColor()<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code has-light-gray-background-color has-background\"><code>int SDL_RenderClear(SDL_Renderer *renderer);\n\n<strong>Returns<\/strong>: 0 on success or a negative error code on failure<\/code><\/pre>\n\n\n\n<p style=\"font-size:1rem\"><code>renderer<\/code> is the rendering context.<\/p>\n\n\n\n<p style=\"font-size:1rem\">Use <code>SDL_RenderFillRect()<\/code> to render a filled rectangle with no outline.<sup><a href=\"#ref\">[6]<\/a><\/sup><\/p>\n\n\n\n<pre class=\"wp-block-code has-light-gray-background-color has-background\"><code>int SDL_RenderFillRect(SDL_Renderer *renderer,\n                       const SDL_Rect *rect);\n\n<strong>Returns<\/strong>: 0 on success or a negative error code on failure<\/code><\/pre>\n\n\n\n<p style=\"font-size:1rem\"><code>renderer<\/code> is the rendering context.<br><code>rect<\/code> is the <code><a href=\"https:\/\/wiki.libsdl.org\/SDL_Rect\">SDL_Rect<\/a><\/code> structure representing the rectangle to fill, or NULL for the entire rendering target.<\/p>\n\n\n\n<pre class=\"wp-block-code has-light-gray-background-color has-background\"><code><a href=\"#ref\">&#091;7]<\/a>\ntypedef struct SDL_Rect\n{\n    int x, y;\n    int w, h;\n} SDL_Rect;<\/code><\/pre>\n\n\n\n<p style=\"font-size:1rem\"><code>x<\/code> and <code>y<\/code> are the x and y values of the rectangle&#8217;s upper left corner.<br><code>w<\/code> and <code>h<\/code> are the width and height of the rectangle, respectively.<\/p>\n\n\n\n<p style=\"font-size:1rem\">Use <code>SDL_RenderDrawRect()<\/code> to draw the outline of a rectangle.<sup><a href=\"#ref\">[8]<\/a><\/sup> Its arguments are identical to the arguments for <code>SDL_RenderFillRect()<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code has-light-gray-background-color has-background\"><code>int SDL_RenderDrawRect(SDL_Renderer *renderer,\n                       const SDL_Rect *rect);\n\n<strong>Returns<\/strong>: 0 on success or a negative error code on failure<\/code><\/pre>\n\n\n\n<p style=\"font-size:1rem\">To destroy a renderer for a window and free associated textures, use <code>SDL_DestroyRenderer()<\/code>.<sup><a href=\"#ref\">[9]<\/a><\/sup><\/p>\n\n\n\n<pre class=\"wp-block-code has-light-gray-background-color has-background\"><code>void SDL_DestroyRenderer(SDL_Renderer *renderer);<\/code><\/pre>\n\n\n\n<p style=\"font-size:1rem\"><code>renderer<\/code> is the rendering context to destroy.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" style=\"font-size:1.75rem\">References<\/h2>\n\n\n\n<ol class=\"wp-block-list\" id=\"ref\" style=\"font-size:1rem\"><li><a href=\"https:\/\/wiki.libsdl.org\/SDL_CreateRenderer\">https:\/\/wiki.libsdl.org\/SDL_CreateRenderer<\/a><\/li><li><a href=\"https:\/\/wiki.libsdl.org\/SDL_CreateTexture\">https:\/\/wiki.libsdl.org\/SDL_CreateTexture<\/a><\/li><li><a href=\"https:\/\/wiki.libsdl.org\/SDL_SetRenderTarget\">https:\/\/wiki.libsdl.org\/SDL_SetRenderTarget<\/a><\/li><li><a href=\"https:\/\/wiki.libsdl.org\/SDL_SetRenderDrawColor\">https:\/\/wiki.libsdl.org\/SDL_SetRenderDrawColor<\/a><\/li><li><a href=\"https:\/\/wiki.libsdl.org\/SDL_RenderClear\">https:\/\/wiki.libsdl.org\/SDL_RenderClear<\/a><\/li><li><a href=\"https:\/\/wiki.libsdl.org\/SDL_RenderFillRect\">https:\/\/wiki.libsdl.org\/SDL_RenderFillRect<\/a><\/li><li><a href=\"https:\/\/wiki.libsdl.org\/SDL_Rect\">https:\/\/wiki.libsdl.org\/SDL_Rect<\/a><\/li><li><a href=\"https:\/\/wiki.libsdl.org\/SDL_RenderDrawRect\">https:\/\/wiki.libsdl.org\/SDL_RenderDrawRect<\/a><\/li><li><a href=\"https:\/\/wiki.libsdl.org\/SDL_DestroyRenderer\">https:\/\/wiki.libsdl.org\/SDL_DestroyRenderer<\/a><\/li><\/ol>\n","protected":false},"excerpt":{"rendered":"<p>This is part 2 in a series on using SDL. See part 1 if you missed it. This post introduces more useful functions, structures, and constants in the SDL 2.0 API. It covers basic details such as return values and &hellip; <a href=\"https:\/\/blogs.oregonstate.edu\/usageoptions\/2022\/05\/12\/using-sdl-part-2\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":12377,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14,15,13,10,12,23,1],"tags":[],"class_list":["post-88","post","type-post","status-publish","format-standard","hentry","category-c","category-capstone-project","category-emulators","category-life","category-school","category-sdl","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blogs.oregonstate.edu\/usageoptions\/wp-json\/wp\/v2\/posts\/88","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.oregonstate.edu\/usageoptions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.oregonstate.edu\/usageoptions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/usageoptions\/wp-json\/wp\/v2\/users\/12377"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/usageoptions\/wp-json\/wp\/v2\/comments?post=88"}],"version-history":[{"count":2,"href":"https:\/\/blogs.oregonstate.edu\/usageoptions\/wp-json\/wp\/v2\/posts\/88\/revisions"}],"predecessor-version":[{"id":90,"href":"https:\/\/blogs.oregonstate.edu\/usageoptions\/wp-json\/wp\/v2\/posts\/88\/revisions\/90"}],"wp:attachment":[{"href":"https:\/\/blogs.oregonstate.edu\/usageoptions\/wp-json\/wp\/v2\/media?parent=88"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/usageoptions\/wp-json\/wp\/v2\/categories?post=88"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/usageoptions\/wp-json\/wp\/v2\/tags?post=88"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}