{"id":290,"date":"2011-09-30T23:22:07","date_gmt":"2011-09-30T23:22:07","guid":{"rendered":"http:\/\/41j.com\/blog\/?p=290"},"modified":"2017-09-17T15:01:37","modified_gmt":"2017-09-17T15:01:37","slug":"simple-sdl-example-in-c","status":"publish","type":"post","link":"https:\/\/41j.com\/blog\/2011\/09\/simple-sdl-example-in-c\/","title":{"rendered":"Simple SDL example in C++"},"content":{"rendered":"<p>This is a simple SDL example. It plots a bunch of random points to the screen. On linux it will require the libsdl and libsdl-dev packages. compile it as follows:<\/p>\n<p>g++ -L\/usr\/lib -lSDL -lpthread rand_points.c -o rand_points<\/p>\n<pre>\r\n#include &lt;string.h&gt;\r\n#include &lt;SDL\/SDL.h&gt;\r\n#include &lt;iostream&gt;\r\n\r\nusing namespace std;\r\n\r\nint main(int argc, char *argv[]) {\r\n\r\n  SDL_Surface *screen;\r\n\r\n  if(SDL_Init(SDL_INIT_VIDEO)&lt;0) {\r\n    cout &lt;&lt; \"Failed SDL_Init \" &lt;&lt; SDL_GetError() &lt;&lt; endl;\r\n    return 1;\r\n  }\r\n\r\n  screen=SDL_SetVideoMode(800,600,32,SDL_ANYFORMAT);\r\n  if(screen==NULL) {\r\n    cout &lt;&lt; \"Failed SDL_SetVideoMode: \" &lt;&lt; SDL_GetError() &lt;&lt; endl;\r\n    SDL_Quit();\r\n    return 1;\r\n  }\r\n\r\n  for(;;) {\r\n    SDL_Flip(screen);\r\n    SDL_LockSurface(screen);\r\n    for(int n=0;n&lt;1000;n++) {\r\n      int x=rand()%800;\r\n      int y=rand()%600;\r\n      int pixel=rand()*100000;\r\n      int bpp = screen-&gt;format-&gt;BytesPerPixel;\r\n      Uint8 *p = (Uint8 *)screen-&gt;pixels + y * screen-&gt;pitch + x * bpp;\r\n      if((x&gt;screen-&gt;w)||(y&gt;screen-&gt;h)||(x&lt;0)||(y&lt;0)) return 0;\r\n      *(Uint32 *)p = pixel;\r\n    }\r\n\r\n    SDL_Event event;\r\n    while(SDL_PollEvent(&amp;event)) {\r\n      if(event.key.keysym.sym == SDLK_c     ) { SDL_FillRect(screen,NULL,0); }\r\n    }\r\n\r\n    SDL_UnlockSurface(screen);\r\n  }\r\n  SDL_Quit();\r\n\r\n  return 0;\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This is a simple SDL example. It plots a bunch of random points to the screen. On linux it will require the libsdl and libsdl-dev packages. compile it as follows: g++ -L\/usr\/lib -lSDL -lpthread rand_points.c -o rand_points #include &lt;string.h&gt; #include &lt;SDL\/SDL.h&gt; #include &lt;iostream&gt; using namespace std; int main(int argc, char *argv[]) { SDL_Surface *screen; if(SDL_Init(SDL_INIT_VIDEO)&lt;0) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[],"class_list":["post-290","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1RRoU-4G","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/290","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/comments?post=290"}],"version-history":[{"count":4,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/290\/revisions"}],"predecessor-version":[{"id":4031,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/290\/revisions\/4031"}],"wp:attachment":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/media?parent=290"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/categories?post=290"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/tags?post=290"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}