2013年8月11日星期日(7。15 色彩动画)

2023-11-19

距离上次封装有一个月了,PHYSX知道怎么弄朝向和位置了,我决定业余时间可以弄弄小游戏,

这个例子是通过改变调色板ID来达到颜色的亮灭,这个肯定过时了,不过思路还是不错的。

各个常量和结构体类型

#define BLINKER_ADD           0    // add a light to database 

#define BLINKER_DELETE        1    // delete a light from database

#define BLINKER_UPDATE        2    // update a light

#define BLINKER_RUN           3    // run normal// blinking light structure

 

typedef struct BLINKER_TYP

               {

               // user sets these

               int color_index;         // index of color to blink

               PALETTEENTRY on_color;   // RGB value of "on" color

               PALETTEENTRY off_color;  // RGB value of "off" color

               int on_time;             // number of frames to keep "on"

               int off_time;            // number of frames to keep "off"

 

               // internal member

               int counter;             // counter for state transitions

               int state;               // state of light, -1 off, 1 on, 0 dead

               } BLINKER, *BLINKER_PTR;

 

主要函数是

 

int Blink_Colors(int command, BLINKER_PTR new_light, int id)

{

// this function blinks a set of lights

 

static BLINKER lights[256]; // supports up to 256 blinking lights

static int initialized = 0; // tracks if function has initialized

 

// test if this is the first time function has ran

if (!initialized)

   {

   // set initialized

   initialized = 1;

 

   // clear out all structures

   memset((void *)lights,0, sizeof(lights));

 

   } // end if

 

// now test what command user is sending

switch (command)

       {

       case BLINKER_ADD: // add a light to the database

            {

            // run thru database and find an open light

            for (int index=0; index < 256; index++)

                {

                // is this light available?

                if (lights[index].state == 0)

                   {

                   // set light up

                   lights[index] = *new_light;

 

                   // set internal fields up

                   lights[index].counter =  0;

                   lights[index].state   = -1; // off

 

                   // update palette entry

                   lpddpal->SetEntries(0,lights[index].color_index,1,&lights[index].off_color);

 

                   // return id to caller

                   return(index);

 

                   } // end if

 

                } // end for index

 

            } break;

 

       case BLINKER_DELETE: // delete the light indicated by id

            {

            // delete the light sent in id

            if (lights[id].state != 0)

               {

               // kill the light

               memset((void *)&lights[id],0,sizeof(BLINKER));

 

               // return id

               return(id);

 

               } // end if

            else

                return(-1); // problem

 

 

            } break;

 

       case BLINKER_UPDATE: // update the light indicated by id

            {

            // make sure light is active

            if (lights[id].state != 0)

               {

               // update on/off parms only

               lights[id].on_color  = new_light->on_color;

               lights[id].off_color = new_light->off_color;

               lights[id].on_time   = new_light->on_time;

               lights[id].off_time  = new_light->off_time;

 

               // update palette entry

               if (lights[id].state == -1)

                  lpddpal->SetEntries(0,lights[id].color_index,1,&lights[id].off_color);

               else

                  lpddpal->SetEntries(0,lights[id].color_index,1,&lights[id].on_color);

 

               // return id

               return(id);

 

               } // end if

            else

                return(-1); // problem

 

            } break;

 

       case BLINKER_RUN: // run the algorithm

            {

            // run thru database and process each light

            for (int index=0; index < 256; index++)

                {

                // is this active?

                if (lights[index].state == -1)

                   {

                   // update counter

                   if (++lights[index].counter >= lights[index].off_time)

                      {

                      // reset counter

                      lights[index].counter = 0;

 

                      // change states

                      lights[index].state = -lights[index].state;               

 

                      // update color

                      lpddpal->SetEntries(0,lights[index].color_index,1,&lights[index].on_color);

 

                      } // end if

                

                   } // end if

                else

                if (lights[index].state == 1)

                   {

                   // update counter

                   if (++lights[index].counter >= lights[index].on_time)

                      {

                      // reset counter

                      lights[index].counter = 0;

 

                      // change states

                      lights[index].state = -lights[index].state;              

 

                      // update color

                      lpddpal->SetEntries(0,lights[index].color_index,1,&lights[index].off_color);

 

                      } // end if

                   } // end else if

                

                } // end for index

 

            } break;

 

       default: break;

 

       } // end switch

 

// return success

return(1);

 

} // end Blink_Colors

在game_init()处,添加了3个id,添加了东东,一为红亮黑灭,二为绿亮黑灭。亮灭都有相应的时间。

 

 

// load the 8-bit image

if (!Load_Bitmap_File(&bitmap,"starshipanim8.bmp"))

   return(0);

 

// load it's palette into directdraw

if (FAILED(lpddpal->SetEntries(0,0,MAX_COLORS_PALETTE,bitmap.palette)))

   return(0);

 

// clean the surface

DDraw_Fill_Surface(lpddsprimary,0);

 

 

// create the blinking lights

 

BLINKER temp; // used to build up lights

 

PALETTEENTRY red   = {255,0,0,PC_NOCOLLAPSE};

PALETTEENTRY green = {0,255,0,PC_NOCOLLAPSE};

PALETTEENTRY black = {0,0,0,PC_NOCOLLAPSE};

 

// add red light

temp.color_index = 253;

temp.on_color    = red;

temp.off_color   = black;

temp.on_time     = 30; // 30 cycles at 30fps = 1 sec

temp.off_time    = 30;

 

// make call, note use of C++ call by reference for 2nd parm

int red_id = Blink_Colors(BLINKER_ADD, &temp, 0);

 

// now create green light

temp.color_index = 254;

temp.on_color    = green;

temp.off_color   = black;

temp.on_time     = 90; // 30 cycles at 30fps = 3 secs

temp.off_time    = 90;

 

// make call, note use of C++ call by reference for 2nd parm

int green_id = Blink_Colors(BLINKER_ADD, &temp, 0);

下面可以看看GAME_MAIN()中怎么操作的。

直接一个

 

// animate the lights

Blink_Colors(BLINKER_RUN, NULL, 0);

看下截图

 

下面进行封装,在ddraw_interfacez.h中加上一个结构体,

typedef struct BLINKER_TYP

               {

               // user sets these

               int color_index;         // index of color to blink

               PALETTEENTRY on_color;   // RGB value of "on" color

               PALETTEENTRY off_color;  // RGB value of "off" color

               int on_time;             // number of frames to keep "on"

               int off_time;            // number of frames to keep "off"

 

               // internal member

               int counter;             // counter for state transitions

               int state;               // state of light, -1 off, 1 on, 0 dead

               } BLINKER, *BLINKER_PTR;

 

加上个成员函数。

int DDRAW_Interface::Blink_Colors(int command, BLINKER_PTR new_light, int id)

只是将lpddpal改为成员变量m_lpddpal。

在main函数中的

 

int Game_Init(void *parms = NULL, int num_parms = 0)

{

     // this is called once after the initial window is created and

     // before the main event loop is entered, do all your initialization

     // here

 

// load the 8-bit image

if (!ddraw->Load_Bitmap_File(&bitmap,"starshipanim8.bmp"))

   return(0);

 

// load it's palette into directdraw

if (FAILED(ddraw->getlpddpal()->SetEntries(0,0,MAX_COLORS_PALETTE,bitmap.palette)))

   return(0);

 

 

 

// create the blinking lights

 

BLINKER temp; // used to build up lights

 

PALETTEENTRY red   = {255,0,0,PC_NOCOLLAPSE};

PALETTEENTRY green = {0,255,0,PC_NOCOLLAPSE};

PALETTEENTRY black = {0,0,0,PC_NOCOLLAPSE};

 

// add red light

temp.color_index = 253;

temp.on_color    = red;

temp.off_color   = black;

temp.on_time     = 30; // 30 cycles at 30fps = 1 sec

temp.off_time    = 30;

 

// make call, note use of C++ call by reference for 2nd parm

int red_id = ddraw->Blink_Colors(BLINKER_ADD, &temp, 0);

 

// now create green light

temp.color_index = 254;

temp.on_color    = green;

temp.off_color   = black;

temp.on_time     = 90; // 30 cycles at 30fps = 3 secs

temp.off_time    = 90;

 

// make call, note use of C++ call by reference for 2nd parm

int green_id = ddraw->Blink_Colors(BLINKER_ADD, &temp, 0);

    

    

// return success or failure or your own return code here

return(1);

}

循环函数也是逐行扫描。

 

 

int Game_Main(void *parms = NULL, int num_parms = 0)

{

// this is the main loop of the game, do all your processing

// here

 

// make sure this isn't executed again

if (window_closed)

   return(0);

 

// for now test if user is hitting ESC and send WM_CLOSE

if (KEYDOWN(VK_ESCAPE))

   {

   PostMessage(main_window_handle,WM_CLOSE,0,0);

   window_closed = 1;

   } // end if

 

     ddraw->DDraw_Lock_Primary_Surface();

// get video pointer to primary surfce

     UCHAR *primary_buffer = (UCHAR *)ddraw->getPrimarybuffer();     

 

// test if memory is linear

if (ddraw->getPrimarylpitch() == SCREEN_WIDTH)

   {

   // copy memory from double buffer to primary buffer

   memcpy((void *)primary_buffer, (void *)bitmap.buffer, SCREEN_WIDTH*SCREEN_HEIGHT);

   } // end if

else

   { // non-linear

 

   // make copy of source and destination addresses

   UCHAR *dest_ptr = primary_buffer;

   UCHAR *src_ptr  = bitmap.buffer;

 

   // memory is non-linear, copy line by line

   for (int y=0; y < SCREEN_HEIGHT; y++)

       {

       // copy line

       memcpy((void *)dest_ptr, (void *)src_ptr, SCREEN_WIDTH);

 

       // advance pointers to next line

        dest_ptr+=ddraw->getPrimarylpitch();

       src_ptr +=SCREEN_WIDTH;

       } // end for

 

   } // end else

ddraw->DDraw_Unlock_Primary_Surface();

// animate the lights

ddraw->Blink_Colors(BLINKER_RUN, NULL, 0);

 

// wait a sec

Sleep(33);

 

// do nothing -- look at pretty picture

 

// return success or failure or your own return code here

return(1);

}

OK,结果一样

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

2013年8月11日星期日(7。15 色彩动画) 的相关文章

随机推荐

  • Hibernate之inverse和cascade详解

    继Hibernate学习笔记整理之后 发现inverse和cascade这两个属性在配置过程中比较含糊 仔细比较一下是有些地方比较像 所以很容易搞糊涂 借助此文来阐述下inverse和cascade的区别 什么是inverse 默认值为fa
  • centos8安装postgresql步骤

    1 安装源 1 sudo yum y install epel release 2 postgresql官网发布的postgresql对应的安装源 sudo yum install y https download postgresql o
  • There are multiple modules with names that only differ in casing.

    问题 在 npm run dev 后 控制台出现警告 没有出现链接 但是在浏览器上直接输入地址http localhost 8080 又可显示界面 There are multiple modules with names that onl
  • 华为机试HJ55 挑7

    HJ55 挑7 Python 题目 解题思路 代码 结果 题目 解题思路 1 多组输入 需要循环 2 循环查找到输入数值即可 字符串查找用in 能否整除 求余后判断是否 0 3 最后打印找到的数字的列表长度 代码 def func n in
  • 逐行对比LLaMA2和LLaMA模型源代码

    几个小时前 2023年7月18日 Meta发布了允许商用的开源模型LLaMA2 笔者逐行对比了LLaMA2模型源代码 和LLaMA相比 几乎没有改动 细节如下 是否改动 LLaMA2 LLaMA 模型整体构架 无 Transformer T
  • 面试必考真题

    1 输入一个链表 反转链表后 输出新链表的表头 package com csu marden public class Demo1 public static void main String args Node head new Node
  • 解决“17: 错误:程序中有游离的‘\240’,\302’

    参考链接 https blog csdn net asuphy article details 54602426 执行如下命令即可 sed i s o240 o302 g dy haikang test cpp
  • Invalid bound statement (not found):

    BUG描述 在执行动态SQL出现问题 原因 mapper接口中的方法名和mapper xml中的id标签不一致 解决方案 修改mapper接口中的方法名 使其对应到mapper xml中的id标签 参考资料
  • openssl的x509命令简单入门

    openssl的x509命令简单入门 openssl是一个强大的开源工具包 它能够完成完成各种和ssl有关的操作 命令说明 openssl help 会得到如下的提示 openssl Error help is an invalid com
  • 数据库常用命令之外键(foreign key)之多对一(总结,基础)

    我是小白 刚接触MySQL不久 现阶段正在学习 为此在CSDN上留下自己的学习笔记 如果有错误的地方还请大家见谅 评论或者私发我错误地方哦 谢谢大家 嘿嘿 此篇将记录外键的相关知识 上篇内容为对一张表的约束条件 传送门 创建表的完整性语法
  • golang基础教程

    目录 golang基础教程 一 环境搭建 golang基础教程 二 开发规范及API golang基础教程 三 变量与数据类型概述 golang基础教程 四 基本数据类型 golang基础教程 五 基本数据类型的转换 golang基础教程
  • 缺少nodejs环境,请在设置中配置nodejs的安装路径 - HBuilder - uniapp

    HBuilder运行uni app项目 点击 运行到 提示 缺少nodejs环境 请在设置中配置nodejs的安装路径 解决办法 找到工具 设置 运行配置 node运行配置 运行终端类型 选择 内置 外部 如果已经配置过 关闭编译器 重新打
  • Unity_如何使相机视角一直跟随角色移动

    实例代码如下 using System Collections using System Collections Generic using UnityEngine 相机视角跟踪 public class FollowTarget Mono
  • QHash & QMap 的顺序问题 (***)

    QT关联容器QMap QHash的Key值自动排序问题 对QMap中的key进行自定义排序 如何取消QMap自动排序 让QMap按照插入的顺序排列 通过插入顺序循环QHash QMap QHash插入后的显示顺序以及记录插入顺序的数据结构
  • OpenGL图形管线和坐标变换

    1 OpenGL 渲染管线 OpenGL渲染管线分为两大部分 模型观测变换 ModelView Transformation 和投影变换 Projection Transformation 做个比喻 计算机图形开发就像我们照相一样 目的就是
  • 最实用的chrome插件

    前言 说真的第一次看到CSDN这个插件的时候并没有感觉特别吸引我的地方 因为我个人安装了好多的插件 第二次看到CSDN插件还是因为广告上说的参赛拿奖 其实我的动机也不是很纯o o 插件地址 CSDN插件 走起 对于谷歌的插件安装方式很多 可
  • springboot——集成elasticsearch进行搜索并高亮关键词

    目录 1 elasticsearch概述 3 springboot集成elasticsearch 4 实现搜索并高亮关键词 1 elasticsearch概述 1 是什么 Elasticsearch 是位于 Elastic Stack 核心
  • C51单片机数码管动态显示

    数码管作为最廉价的输出设备 在各种自动化设备中有很大的应用 最简单普通的显示方式为动态刷新显示 称为假动态显示 即通过分时扫描每一位 利于人眼的视觉停留现象 造成一种静态显示的效果 如下图所示 C51单片机由于运行速度很慢 在高刷新频率下
  • ASCII与C简单数据类型

    ascII与简单数据类型 1 打印出所有ascII表中的字符 思路 ascII码值与阿拉伯数字0 127对应 故可以先将其以数字形式存在数组空间或内存空间中 然后用 c一个一个打印出来即可 本次采用数组进行存储 代码 include
  • 2013年8月11日星期日(7。15 色彩动画)

    距离上次封装有一个月了 PHYSX知道怎么弄朝向和位置了 我决定业余时间可以弄弄小游戏 这个例子是通过改变调色板ID来达到颜色的亮灭 这个肯定过时了 不过思路还是不错的 各个常量和结构体类型 define BLINKER ADD 0 add