Hello humans. First things first, I’m using SDL2.0.8.
I’ll be brief: I’m trying to find a way to draw a smaller surface onto the window’s surface (for pixel art games), but without having to ditch the SDL_Render routine set.
Here is a snippet to show what I’m trying to do:
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 128
#define SCALE 4
int main(int argc, char **argv) {
/* Skipping mundale loading stuff */
SDL_Window *window = SDL_CreateWindow(
"SDL Game",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH * SCALE, SCREEN_HEIGHT * SCALE,
SDL_WINDOW_SHOWN
);
SDL_Surface *windowSurface = SDL_GetWindowSurface(window);
SDL_Surface *renderTarget = SDL_CreateRGBSurface(0, SCREEN_WIDTH, SCREEN_HEIGHT, 32, 0, 0, 0, 0);
SDL_Renderer *renderer = SDL_CreateSoftwareRenderer(renderTarget);
IMG_Init(IMG_INIT_PNG);
SDL_Surface *surfPlayer = IMG_Load("res/img/my_sprite.png");
SDL_Texture *player = SDL_CreateTextureFromSurface(renderer, surfPlayer);
SDL_FreeSurface(surfPlayer);
SDL_Rect playerFrame = { 0, 0, 8, 8 };
SDL_Rect playerRect = { 0, 0, playerFrame.w, playerFrame.h };
bool running = true;
SDL_Event e;
while (running) {
while(SDL_PollEvent(&e)) {
switch (e.type) {
case SDL_WINDOWEVENT_CLOSE:
case SDL_QUIT:
running = false;
case SDL_KEYDOWN:
if (e.key.keysym.sym == SDLK_q)
running = false;
}
}
SDL_SetRenderDrawColor(renderer, 0x80, 0x80, 0x80, 0xff);
SDL_RenderClear(renderer);
SDL_RenderCopyEx(renderer, player, &playerFrame, &playerRect, 0, NULL, SDL_FLIP_HORIZONTAL);
SDL_BlitScaled(renderTarget, NULL, windowSurface, NULL);
SDL_UpdateWindowSurface(window);
}
SDL_DestroyTexture(player);
SDL_DestroyWindow(window);
SDL_FreeSurface(renderTarget);
SDL_DestroyRenderer(renderer);
return EXIT_SUCCESS;
}
This only works with textures that doesn’t have an alpha channel. If they do, they get mixed up.
(I tried 8 and 32bit depths for images, without success)
Per-pixel stuff is faster with surfaces, this the only reason I’m not using SDL_Renderer in hardware mode.
(Sorry for not being so polite, I’m in quite of a rush in my friend’s place)
Thanks in advance, and thanks for all the community for the great work you all have been doing, just terrific!