Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

ZwodahS

Member Since 11 May 2010
Offline Last Active Yesterday, 03:16 AM
-----

#5053110 SDL multiple layer transparency issue

Posted by ZwodahS on 14 April 2013 - 05:15 AM

Thanks a lot. 

 

Here is the code that works, in case other people face the same problem.

#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include <iostream>


SDL_Surface* loadImage(std::string filename)
{
    SDL_Surface* img = IMG_Load(filename.c_str());
    if(img == NULL)
    {
        return NULL;
    }
    SDL_Surface* optimized = SDL_DisplayFormatAlpha(img);
    SDL_FreeSurface(img);
    return optimized;
}

int main(int args , char* argv[] )
{
    if(SDL_Init(SDL_INIT_EVERYTHING) == 1)
    {
        std::cout << "error" << std::endl ;
    }
    SDL_Surface* surface = SDL_SetVideoMode(100,100,32,SDL_SWSURFACE);
    SDL_FillRect(surface,0,SDL_MapRGBA(surface->format,100,100,100,255));

    SDL_Surface* circle = loadImage("circle.png");
    SDL_Surface* square = loadImage("square.png");

    SDL_PixelFormat* format = surface->format;
    SDL_Surface* temp1 = SDL_CreateRGBSurface(SDL_SWSURFACE,100,100,32,format->Rmask,format->Gmask,format->Bmask,format->Amask);
    SDL_Surface* temp2 = SDL_CreateRGBSurface(SDL_SWSURFACE,100,100,32,format->Rmask,format->Gmask,format->Bmask,format->Amask);

    temp1 = SDL_DisplayFormatAlpha(temp1);
    temp2 = SDL_DisplayFormatAlpha(temp2);

    SDL_SetAlpha(square,0,square->format->alpha);
    SDL_SetAlpha(circle,0,circle->format->alpha);

    SDL_Rect rect = {0 ,0 , 100 , 100};

    SDL_BlitSurface(square,&rect,temp1,&rect);
    SDL_BlitSurface(circle,&rect,temp2,&rect);
    SDL_BlitSurface(temp1,&rect,surface,&rect);
    SDL_BlitSurface(temp2,&rect,surface,&rect);

    SDL_Flip(surface);


    SDL_Delay(2000); 

 

SDL_DisplayFormatAlpha is needed to set the mask for the surface, because the original surface do not have alpha mask.

 

There is a lot of memory leak here, and many surface is not freed to reduce the length of the code.

 

Once again, thanks :P




#5052190 SDL multiple layer transparency issue

Posted by ZwodahS on 11 April 2013 - 12:16 PM

Hi guys I have another issue with SDL recently. Here is the smallest segment possible.

Circle is a circle with a hole in the middle and square is just a square.

SDL_Surface* loadImage(std::string filename)
{
    SDL_Surface* img = IMG_Load(filename.c_str());
    if(img == NULL)
    {
        return NULL;
    }
    SDL_Surface* optimized = SDL_DisplayFormatAlpha(img);
    SDL_FreeSurface(img);
    return optimized;
}

int main(int args , char* argv[] )
{
    if(SDL_Init(SDL_INIT_EVERYTHING) == 1)
    {
        std::cout << "error" << std::endl ;
    }
    SDL_Surface* surface = SDL_SetVideoMode(100,100,32,SDL_SWSURFACE);
    SDL_Surface* circle = loadImage("circle.png");
    SDL_Surface* square = loadImage("square.png");
    SDL_PixelFormat* format = surface->format;    
    SDL_Rect rect = {0 ,0 , 100 , 100};

    // draw code
    
    SDL_Flip(surface);


 

so for the draw code , this works.

    SDL_BlitSurface(square,&rect,surface,&rect);
    SDL_BlitSurface(circle,&rect,surface,&rect);

However, I want to draw to a temporary surface first before drawing.

Then this don't work.

    SDL_PixelFormat* format = surface->format;
    SDL_Surface* temp1 = SDL_CreateRGBSurface(SDL_SWSURFACE,100,100,32,format->Rmask,format->Gmask,format->Bmask,format->Amask);
    SDL_Surface* temp2 = SDL_CreateRGBSurface(SDL_SWSURFACE,100,100,32,format->Rmask,format->Gmask,format->Bmask,format->Amask);

    SDL_BlitSurface(square,&rect,temp1,&rect);
    SDL_BlitSurface(circle,&rect,temp2,&rect);
    SDL_BlitSurface(temp1,&rect,surface,&rect);
    SDL_BlitSurface(temp2,&rect,surface,&rect);

 

When I do this, the temporary surface has a black background and the final output is only a circle.

 

I checked and I found out that surface->format->Amask is 0 , so the created surface do not have alpha enabled.

So instead of using surface-> format, i tried using circle->format instead. The result ? , nothing is drawn.

 

I tried other stuffs as well, like doing a SDL_FillRect onto temp1 and temp2 , but it still doesn't do what i want. =( 

I tried doing SDL_SetAlpha as well but still didn't work. What am i missing ? =/

 




#4990716 Would you play this game ?

Posted by ZwodahS on 16 October 2012 - 07:59 AM

I always wanted to make a survival resource game, where players are required to gather resource to survive. However, I really have no idea how the game would be like or how to go out of the box to create a interesting mechanic to make, until now.

I started drafting ideas about 4 days ago, played it on paper and coded a small prototype. I thought i stumble upon something interesting and wonder if anyone will like this game mechanic.

The game has a turn aspect and a real time aspect. The game take placed in a grid like world like most CIv game. You control a tribe that needs to move from a tile on the map to another tile on the map. However, each turn you can only move a short distance and moving require food.

Each turn also contains a real time part, where you send out units to harvest from surrounding tiles. You can't control the unit directly but can only place flags to issue a command to your units to move there and perform action. Currently there is only 3 flag ( Gather , Scout , Hunt).

So there is the mechanic that I want to try. Most games like this would provide a "sure" event where you would get the resource you want or a "random" event that you have no control over.

In this game, instead of providing random event , the game is based around the idea of cards. I would make this into a board game but I would need to print a few thousand cards, so making a video game out of it seems to be more feasible.

So each tile would have a deck of cards. The deck would contains cards like "food", "wood" and other resources, as well as hazards like animals(lion,tiger) or other events. In order to get the card you need to issue a "Gather" command to the tile. A gatherer will turn go to the tile and draw a card and return. If the gatherer draw a hazard, then other stuffs will happen. For example, if Lion is drawn, then perhaps a combat would take place. Gatherer will not have the ability to kill Lion so they can only run.

A Hunt command would perhaps Draw 5 cards, and If there is any animals in those 5 cards, a combat will be initiated.

However, most deck will also be filled with "Nothing Found" cards. This will be a waste of time for the gatherer. The job is left to the Scout, who will draw a large number of cards and remove all the "Nothing Found" and at the same time revealing what hazard is in the tile.

Each turn last 1 minutes and at the end of each turn, the tribe will consume food and if there is not enough food, the player lose. At the end of the turn, the player can decide where to relocate the base camp. Moving base camp will cost food.

Interesting aspect of this card mechanics :

1) Player will not know the probability of what is in the deck but at the same time, could guess what is in the deck. For example, the game would be programmed in such a way that "Forest" tile will contains more food than desert.
2) The probability of getting food can be adjusted to your favor if you send out scouts first. Revealing what is in tile allows the player to see what are the risk and rewards and make decision based on probability.
3) As the resource on a tile depletes, future visit to the tile will provided less "Expected Value". This will encourage the player to move fast.
4) Placing upgrades in the deck will encourage player to try get those upgrades. For example, upgrade can be technology that allow player to cook their meat which will provide more food value. This will encourage player to explore and take the risk in staying in an area.


I am intending the game to be played over a short time ( 20 turns for a small map, 40 turns for a bigger map). The game will start with a negative gain on food, meaning that the player will have a starting amount of food which will deplete fast if the player cannot find "upgrades" and additional food sources etc.

The game will feel a bit RTS-ish but also a bit strategic. You plan what you want to know before you start the turn. During the turn, if you accidentally turn over a lion card on a tile and feel that it is not worth risking gatherer to that tile, you can react and send units to other tiles.

So how do you feel about this idea , and would you play such a game ?


PARTNERS