Ejemplo n.º 1
0
void renderRectBorder(SDL_Renderer* renderer, Coordinate a, Coordinate b, SDL_Color color) {
	SDL_Rect drawRect;
	
	drawRect.x = a.x;
	drawRect.y = a.y;
	drawRect.w = b.x-a.x;
	drawRect.h = b.y-a.y;

	SetRenderDrawColor(renderer, color);

	SDL_RenderDrawRect(renderer, &drawRect);
}
Ejemplo n.º 2
0
bool init() {
    bool success = true;

    // Initialize SDL
    if ( SDL_Init(SDL_INIT_VIDEO) < 0 )
    {
        printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
        success = false;
    } else {
        // Create window
        /* Signature: (window caption, window x position,
                    window y position, window width, window height,
                    window is visible)
        */
        gWindow = SDL_CreateWindow("Hello World", SDL_WINDOWPOS_UNDEFINED,
                SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT,
                SDL_WINDOW_SHOWN);
        if (gWindow == NULL) {
            printf("SDL window could not be created! SDL_Error: %s\n", SDL_GetError());
            success = false;
        } else {
            // Create a renderer for window
            /* Signature:
               (Window, index of rendering driver or first one supporting flags [-1], requirement flags)
            */
            gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED);

            if (gRenderer == NULL)
            {
                printf("Renderer could not be created! SDL Error: %s\n", SDL_GetError());
                success = false;
            }
            else {
                // Initialize renderer color
                /* Draw color to rendering context, r(ed), g(reen), b(lue), a(lpha) */
                SetRenderDrawColor(gRenderer, BASE03);

                // Initialize PNG loading (separate SDL2 library)
                int imgFlags = IMG_INIT_PNG;
                if ( !(IMG_Init(imgFlags) & imgFlags )) {
                    printf("SDL_image could not be initialized! SDL_Error: %s\n", IMG_GetError());
                    success = false;
                } else {
                    // Get window surface
                    gScreenSurface = SDL_GetWindowSurface(gWindow);
                }
            }
        }
    }
    return success;
}
Ejemplo n.º 3
0
void renderEllipse(SDL_Renderer* renderer, Coordinate center, int a, int b, SDL_Color color) {
	SetRenderDrawColor(renderer, color);

	int xc = center.x;
	int yc = center.y;
	int a2 = a * a;
	int b2 = b * b;
	int twoa2 = 2 * a2;
	int twob2 = 2 * b2;
	int p;
	int x = 0;
	int y = b;
	int px = 0;
	int py = twoa2 * y;

	/* Plot the initial point in each quadrant. */
	ellipsePlotPoints(renderer, xc, yc, x, y);

	/* Region 1 */
	p = std::round (b2 - (a2 * b) + (0.25 * a2));
	while (px < py) {
		x++;
		px += twob2;
		if (p < 0)
			p += b2 + px;
		else {
			y--;
			py -= twoa2;
			p += b2 + px - py;
		}
		ellipsePlotPoints(renderer, xc, yc, x, y);
	}

	/* Region 2 */
	p = std::round (b2 * (x+0.5) * (x+0.5) + a2 * (y-1) * (y-1) - a2 * b2);
	
	while (y > 0) {
		y--;
		py -= twoa2;
		if (p > 0)
			p += a2 - py;
		else {
			x++;
			px += twob2;
			p += a2 - py + px;
		}
		ellipsePlotPoints(renderer, xc, yc, x, y);
	}
}
Ejemplo n.º 4
0
void renderLine(SDL_Renderer* renderer, Coordinate a, Coordinate b, SDL_Color color) {
	SetRenderDrawColor(renderer, color);

	SDL_RenderDrawLine(renderer, a.x, a.y, b.x, b.y);
}
Ejemplo n.º 5
0
// SDL REQUIRES this signature for main
int main(int argc, char* args[]) {

    // Main Loop flag
    bool quit = false;

    int render = 0;

    // Event handler
    SDL_Event e;

    //Start up SDL and create window
    if( !init() )
    {
        printf("Failed to initialize!\n");
        return 1;
    }
    else
    {
        //Load media
        if( !loadMedia() )
        {
            printf("Failed to load media!\n");
            return 1;
        }
    }

    gTexture = gKeyPressTextures[KEY_PRESS_SURFACE_DEFAULT];

    SDL_Rect lowerViewport;
    lowerViewport.x = 0;
    lowerViewport.y = SCREEN_HEIGHT / 2;
    lowerViewport.w = SCREEN_WIDTH ;
    lowerViewport.h = SCREEN_HEIGHT / 2;

    SDL_Rect topViewport;
    topViewport.x = 0;
    topViewport.y = 0;
    topViewport.w = SCREEN_WIDTH;
    topViewport.h = SCREEN_HEIGHT / 2;

    SDL_Rect fullViewport;
    fullViewport.x = 0;
    fullViewport.y = 0;
    fullViewport.w = SCREEN_WIDTH;
    fullViewport.h = SCREEN_HEIGHT;

    // While application is running
    while (!quit) {

        // Handle events on queue
        while (SDL_PollEvent(&e) != 0) {

            // User requests quit
            if (e.type == SDL_QUIT || e.key.keysym.sym == SDLK_ESCAPE) {
                quit = true;
            }

            // User presses a key
            else if (e.type == SDL_KEYDOWN)
            {
                switch(e.key.keysym.sym)
                {
                    default:
                    render = RENDER_NULL;
                    break;

                    case SDLK_h:
                    gTexture = gKeyPressTextures[KEY_PRESS_SURFACE_LEFT];
                    render = RENDER_SHAPE;
                    break;

                    case SDLK_j:
                    gTexture = gKeyPressTextures[KEY_PRESS_SURFACE_DOWN];
                    render = RENDER_SHAPE;
                    break;

                    case SDLK_k:
                    gTexture = gKeyPressTextures[KEY_PRESS_SURFACE_UP];
                    render = RENDER_SHAPE;
                    break;

                    case SDLK_l:
                    gTexture = gKeyPressTextures[KEY_PRESS_SURFACE_RIGHT];
                    render = RENDER_SHAPE;
                    break;

                    case SDLK_SPACE:
                    gTexture = gKeyPressTextures[KEY_PRESS_SURFACE_SPACE];
                    render = RENDER_SHAPE;
                    break;

                    case SDLK_c:
                    render = RENDER_CIRCLE;

                }
            }
        } //  while (SDL_PolLEvent(&e) != 0

        if (render != RENDER_NULL) {

            // Clear screen
            SDL_RenderClear(gRenderer);

            switch(render)
            {
                case RENDER_CIRCLE:
                // Render image of circle via LTexture
                SDL_RenderSetViewport(gRenderer, &fullViewport);
                gBackgroundTexture.render(0, 0);
                gCircleTexture.render(240, 190);
                break;


                case RENDER_SHAPE:
                // Render image to screen via texture set previously
                SDL_RenderSetViewport(gRenderer, &topViewport);
                SDL_RenderCopy(gRenderer, gTexture, NULL, NULL);

                SDL_RenderSetViewport(gRenderer, &lowerViewport);
                //Render red filled quad
                SDL_Rect fillRect = {SCREEN_WIDTH / 4, SCREEN_HEIGHT / 4, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2};
                SetRenderDrawColor(gRenderer, BASE01);
                SDL_RenderFillRect( gRenderer, &fillRect );

                // Draw a rectanglular outline with empty center
                SDL_Rect outlineRect = {SCREEN_WIDTH / 6, SCREEN_HEIGHT / 6, SCREEN_WIDTH * 2 / 3, SCREEN_HEIGHT * 2 / 3 };
                SetRenderDrawColor(gRenderer, BASE03);
                SDL_RenderDrawRect(gRenderer, &outlineRect);

                // Draw a thin pixel line
                SetRenderDrawColor(gRenderer, BLUE);
                SDL_RenderDrawLine(gRenderer, 0, SCREEN_HEIGHT / 2, SCREEN_WIDTH, SCREEN_HEIGHT / 2);

                // Draw vertical line of cyan dots
                SetRenderDrawColor(gRenderer, CYAN);
                for (int i = 0; i < SCREEN_HEIGHT; i += 4) {
                    SDL_RenderDrawPoint(gRenderer, SCREEN_WIDTH / 2, i);
                }

                SetRenderDrawColor(gRenderer, BASE03);

                break;

            } // render switch

            // Update screen
            SDL_RenderPresent(gRenderer);

        } // if render != null
    } // while (!quit)

    // Free up resources and close SDL
    close();

    return 0;
}