Code covered by the BSD License  

Highlights from
Stellaria (Tech Demo) - The best MATLAB shooting game ever

image thumbnail

Stellaria (Tech Demo) - The best MATLAB shooting game ever

by

 

14 May 2011 (Updated )

If you think you have played the best MATLAB game before. Think again after you try this one.

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

stl_addToGlobalImageHandles.m
function [addedImageIndices, addedImageHandles, nImages] = stl_addToGlobalImageHandles(nImages, imagesToAdd)
%% STL_ADDTOGLOBALIMAGEHANDLES - Add several images to the global image
% queue (ͼȫͼζ)
%
% 
%% USAGE ÷:
% ------
% [addedImageIndices, addedImageHandles] = stl_addToGlobalImageHandles(nImages, imagesToAdd)
% 
%% Input 
% ------
% nImages: The number of images to add.d
% imagesToAdd(unused): Contains the graphical content of the images to add, an 1 X
%               nImages struct that contains the following fields
%              ҪӵͼεݣΪ1 x nImages СĽṹ顣 
%               - CData
%               - AlphaData
%               - XData
%               - YData
%
%% Output :
% -------
% addedImageIndices: The indices of the image handles which contain the
%                       added images, must be an 1 x nImages (output) cell array
%                       ȫͼζУЩոձͼε±. Ϊ
%                       1 x nImages (ֵ)Ԫ
% addedImageHandles: The actual handles of these images size and type
%                       ditto
%                        ЩոձͼεľֵСͬ
% nImages: The actual number of images added, equal or lower than the input
%           nImages
%           ʵʼͼεĸضСڻֵnImages
% 
%% Dependencies ϵ:
% -------
% -- Global Variables Created:
%       none
% -- Global Variables Used:
%       queueGlobalImageHandles
%       numAvailGlobalImageHandles
%       availGlobalImageHandles
% -- Global Variables Modifed:
%       queueGlobalImageHandles
%       numAvailGlobalImageHandles
%       availGlobalImageHandles
% -- Functions Used:
%       none
%
%% Created by Mingjing Zhang, Stellari Studio, 2011.

%% Update History
%
%  Date                     Updater                 Modifications Made
% --------------            -----------------       --------------------
%  Mar 13, 2011             M. Zhang                wrote it
%  Mar 15, 2011             M. Zhang                made imagesToAdd obsolete
% --------------            -----------------       --------------------

%% --------------------Code Starts Here -------------------------

%% Declare the Global Variables ȫֱ
global queueGlobalImageHandles
global numAvailGlobalImageHandles
global availGlobalImageHandles

%% Exception Processing ⴦
% If the number of available images is smaller than the number of images to
% add, then add only the number of available images.
% ȫֶУʹõimageĿСҪӵͼĿôֻȫֶ
% Ϊֹimageʾ
nImages = min(nImages, numAvailGlobalImageHandles);

% If no image is added at all, then return immediately with empty return
% values.
% ûͼαӣôؿվ
% If not, create the return value with the given size.
% ôԤؾ
if nImages <= 0
    addedImageIndices = [];
    addedImageHandles = [];
    return;
else
    addedImageIndices = zeros(1, nImages);
    addedImageHandles = zeros(1, nImages);
end

%% Main Code

% Put nImages available handles to a temporary array.
% * Not really necessary here, but may be useful if the mechanism of
% * queueing were to be altered in a future version. 
% ҳǰnImagesõHandlesҽʱС
% * ʵһDZġĴиı˶еʵַʽ
% * ôдܾDZҪˡ
tempGlobalImageHandles = availGlobalImageHandles(1:nImages);

% -------------- Maybe it is not a good idea to initialize them here ----
% Set the properties of the images, one by one.
% % image
% % for i = 1:nImages
% %     set(queueGlobalImageHandles(tempGlobalImageHandles(i)), ...
% %         'CData', imagesToAdd(i).CData,...
% %         'AlphaData', imagesToAdd(i).AlphaData...
% %         );
% % end

% Get the indices and handles;
% ·ֵϢ
% addedImageIndices = tempGlobalImageHandles;
% addedImageHandles = queueGlobalImageHandles(tempGlobalImageHandles);

% Turn on the images added
% ʹ¼ӵimagesɼ
set(queueGlobalImageHandles(tempGlobalImageHandles), 'Visible', 'on');
addedImageIndices = num2cell(tempGlobalImageHandles);
addedImageHandles = num2cell(queueGlobalImageHandles(tempGlobalImageHandles));

% Update the number of available image handles
% ¿ͼξĿ
numAvailGlobalImageHandles = numAvailGlobalImageHandles - nImages;

% delete the used values, and pad the unused elements with zero.
% aGIHɾռõЩʣԪ㲹롣
availGlobalImageHandles(1:numAvailGlobalImageHandles) = ...
    availGlobalImageHandles((1:numAvailGlobalImageHandles)+nImages);
availGlobalImageHandles(numAvailGlobalImageHandles+1:end) = 0;
    
%% --------------- Code Ends Here --------------------------------
return;

Contact us