The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
help, sdl based boilerplate code for rasterizor
rpsrps


Joined: 19 Oct 2015
Posts: 2
hi,
i am new to sdl.
i am trying to make a portable triangle rasterizor / software renderer for educational purpose.

the prototype planned is a rotating color interpolated triangle about x,y or z axes.
where can i find boiler plate code for this.

all i need is a way to do bit block transfer of pixels from heap to sdl video buffer without flickering problem.

there are tutorials,
but i have never seen any software triangle renderer / rasterizor made in sdl2.

thanks in advance for reading this silly question and for your time.
i need the code similar to this
rpsrps


Joined: 19 Oct 2015
Posts: 2
i have written code referring to a project based on libsdl 1.x and got this error while building on visual studio.
" error C3861: 'SDL_Flip': identifier not found"

please help me to port this code to libsdl2
Code:

#include<SDL.h>
#include<stdio.h>
#include<math.h>

#define SCREEN_WIDTH  600
#define SCREEN_HEIGHT 480

SDL_Window *window = NULL;
SDL_Surface *screen = NULL;
SDL_Event event;

int init();
void render();
void run();
void close();

int init()
{
   int errorcode;
   const char *errorstring;

   //Initialize SDL
   errorcode = SDL_Init(SDL_INIT_VIDEO);
   if( errorcode < 0 ) {
      errorstring = SDL_GetError();
      printf( "\nSDL_Init(...) failed!; SDL_Error: %s", errorstring );
      return -1;
   }
   
   //Create SDL Window
   window = SDL_CreateWindow( "Software_Renderer_BoilerPlate",
      SDL_WINDOWPOS_UNDEFINED,
      SDL_WINDOWPOS_UNDEFINED,
      SCREEN_WIDTH,
      SCREEN_HEIGHT,
      SDL_WINDOW_SHOWN );
   if( window == NULL ) {
      errorstring = SDL_GetError();
      printf( "\nSDL_CreateWindow(...) failed!; SDL_Error: %s", errorstring );
      return -1;
   }

   //Get a pointer to SDL window video memory
   screen = SDL_GetWindowSurface( window );

   return 0;
}

void close()
{
   SDL_DestroyWindow( window );
   window = NULL;
   SDL_Quit();
}

void render(void)
{   
    Uint8 *p;
   for(int i=SCREEN_HEIGHT-1; i>=0; --i)
    {
        for(int j=SCREEN_WIDTH-1;j>=0; --j)
        {
            p = (Uint8 *)screen->pixels +
            ((SCREEN_WIDTH - 1) - j) * screen->pitch +
            i * screen->format->BytesPerPixel;
            *p=(Uint8)(0xFF);
            p++;
            *p=(Uint8)(0xFF);
            p++;
            *p=(Uint8)(0xFF);
        }
    }
}



void run()
{
    bool running = true;
   printf("\nPress escape to stop rendering.");
    while (running) {
      render();
      SDL_Flip(screen);

        while(SDL_PollEvent(&event)) {
            if (event.type == SDL_KEYDOWN) {
                if (event.key.keysym.sym == SDLK_ESCAPE) {
                    running = false;
                }
            } else if(event.type == SDL_QUIT) {
                running = false;
            }
        }
    }
}


int main(int argc, char** argv)
{
   if( init() == 0) {
      run();
   }
   return 0;
}