标题:为何加载烟花就换了一句,将图片加载进资源,结果运行中烟花不显示
取消只看楼主
棋行天下黄
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2017-5-7
结帖率:0
 问题点数:0 回复次数:0 
为何加载烟花就换了一句,将图片加载进资源,结果运行中烟花不显示
为何将图片装载在资源中编译没任何错误,程序也可基本运行,资源中的图片不工作
#include <stdio.h>    //标准的输入输出头文件
#include <math.h>
#include <graphics.h> //图形库头文件
#include <mmsystem.h>
#pragma comment(lib,"winmm.lib")
#include "resource.h"
#include <conio.h>


#define NUM 13
#define  PI  3.1415926548

//烟花弹结构体
struct Jet
{
    int x; //坐标
    int y;
    int hx; //最高的坐标 将赋值给烟花  
    int hy;
    int height; //高度
    bool shoot; //是否可以发射
    DWORD t1, t2, dt;//烟花弹的上升速度
    IMAGE img[2];    //一亮一暗两张图片
    byte n : 1;      //图片下标 0 1  //位段 一个二进制位  int 32 float 32 short 16
}jet[13];

//烟花结构体
struct Fire
{
    int x, y;            //坐标
    int cen_x, cen_y;    //中心坐标
    int width,height;    //图片的宽和高
    int r;                //半径 从小变大
    int max_r;            //最大的半径

    int xy[240][240];   //像素点的集合

    bool show;
    bool draw;            //输出像素点
    DWORD t1, t2, dt;
}fire[NUM];

void Init(int i);
void welcome();
void Load();
void Chose(DWORD& t1);
void shoot();
void Show(DWORD* pMem);


int main()
{
    initgraph(1200, 800); //打开一个界面
    welcome();

    DWORD t1 = timeGetTime();
    DWORD *pMem = GetImageBuffer();

    //循环初始化13次
    for (int i = 0; i < NUM; i++)
    {
        Init(i);
    }
    Load();
    BeginBatchDraw();

    while (!_kbhit())
    {
        Sleep(10);

        for (int clr = 0; clr < 1000; clr++)
        {
            for (int j = 0; j < 2; j++)
            {
                int px1 = rand() % 1200;
                int py1 = rand() % 800;

                if (py1 < 799)
                {
                    pMem[py1 * 1200 + px1] = pMem[py1 * 1200 + px1 + 1] = BLACK;
                }
            }
        }

        Chose(t1);
        shoot();
        Show(pMem);
        FlushBatchDraw(); //吧显存展示出来
    }

    getchar();
    closegraph();        //关闭界面
    return 0;
}


void welcome()
{
    //wav
    //PlaySound((LPCTSTR)IDR_WAVE1, NULL, SND_RESOURCE | SND_LOOP | SND_ASYNC);
    //MP3
    mciSendString(L"open ./music/你还要我怎样.mp3 alias BGM", 0, 0, 0);
    mciSendString(L"play BGM repeat", 0, 0, 0);
    settextcolor(YELLOW);

    //怎样得到一个圆的轨迹上的n个坐标。
    for (int i = 0; i < 50; i++)
    {
        int x = 600 + int(180 * sin(PI * 2 * i / 60));
        int y = 200 + int(180 * cos(PI * 2 * i / 60));
        cleardevice();  //刷新 没有指定颜色 默认为黑色
        settextstyle(i, 0, L"楷体");
        outtextxy(x - 200, y, L"给:");
        outtextxy(x + 50, y + 100, L"某某");
        Sleep(25);
    }
    getchar(); //卡住

    cleardevice();
    settextstyle(25, 0, L"楷体");
    outtextxy(400,150,L"XXXXXXXXXXXXXXXXXXX");
    outtextxy(400, 150, L"XXXXXXXXXX");
    outtextxy(400, 200, L"XXXXXXXXX");
    outtextxy(400, 250, L"XXXXXXXXXXXXXXXXXX");
    outtextxy(400, 300, L"XXXXXXXXXXXXXXXXXXXXXXXXXXX");
    outtextxy(400, 350, L"XXXXXXXXXXXXXXXXXX");
    outtextxy(400, 400, L"XXXXXXXXX");
    outtextxy(400, 450, L"XXXXXXXXXXXXXXXXXXXXXXXXXXX");
    outtextxy(400, 500, L"XXXXXXXXXXXXXXXXXX");
   
    getchar();
}


void Init(int i)
{
    int r[13] = { 120, 120, 155, 123, 130, 147, 138, 138, 130, 135, 140, 132, 155 };
    int x[13] = { 120, 120, 110, 117, 110, 93, 102, 102, 110, 105, 100, 108, 110 };
    int y[13] = { 120, 120, 85, 118, 120, 103, 105, 110, 110, 120, 120, 104, 85 };

    //初始化烟花
    fire[i].x = 0;
    fire[i].y = 0;
    fire[i].width = 240;
    fire[i].height = 240;
    fire[i].max_r = r[i];
    fire[i].cen_x = x[i];
    fire[i].cen_y = y[i];
    fire[i].show = false;
    fire[i].dt = 5;
    fire[i].t1 = timeGetTime();
    fire[i].r = 0;


    //初始化烟花弹
    jet[i].x = 0;
    jet[i].y = 0;
    jet[i].hx = 0;
    jet[i].hy = 0;
    jet[i].height = 0;
    jet[i].t1 = timeGetTime();
    jet[i].dt = rand() % 10;
    jet[i].n = 0;
    jet[i].shoot = false;

   
}


void Load()
{
    //加载烟花
    IMAGE fm,gm;
    //loadimage(&fm, L"./images/bluexc/flower.jpg");//用这一句,使用文件中的图片可以显示
    loadimage(&fm, RT_BITMAP, MAKEINTRESOURCE(IDB_BITMAP2));//换这一句,资源中的图片无法显示,请帮忙修改能运行,我需要将图片装载在资源中,说明程序运行需要装EASYX运行图新界面库
    for (int i = 0; i < NUM; i++)
    {
        SetWorkingImage(&fm);  //工作---对象是一张图片
        getimage(&gm, i * 240, 0, 240, 240);

        SetWorkingImage(&gm);  //工作---对象是一张图片
        for (int a = 0; a < 240; a++)
        {
            for (int b = 0; b < 240; b++)
            {
                fire[i].xy[a][b] = getpixel(a, b);
            }
        }
    }

    //加载烟花弹
    IMAGE jm;
    loadimage(&jm, L"./images/shoot.jpg");
    //jm 里面保存了一整张图片 分隔开来

    for (int i = 0; i < NUM; i++)
    {
        SetWorkingImage(&jm);  //工作---对象是一张图片
        int n = rand() % 5;    //0-4

        getimage(&jet[i].img[0], n * 20, 0, 20, 50);
        getimage(&jet[i].img[1], (n + 5) * 20, 0, 20, 50);
    }

    SetWorkingImage(NULL); //放弃掉刚才的工作区域。
    //零基础 条件选择  循环。
}


//选出13个
void Chose(DWORD& t1)
{
    DWORD t2 = timeGetTime();

    if (t2 - t1 > 100)
    {
        int n = rand() % 20;

        if (n < NUM&&jet[n].shoot == false&&fire[n].show==false)
        {
            jet[n].x = rand() % 1200;
            jet[n].y = rand() % 100 + 600; //600-699
            jet[n].hx = jet[n].x;
            jet[n].hy = rand() % 400;
            jet[n].height = jet[n].y - jet[n].hy;
            jet[n].shoot = true;
            putimage(jet[n].x, jet[n].y, &jet[n].img[jet[n].n], SRCINVERT);
        }
        t1 = t2;
    }
}


void shoot()
{
   

    for (int i = 0; i < NUM; i++)
    {
        jet[i].t2 = timeGetTime();
        if (jet[i].t2 - jet[i].t1>jet[i].dt&&jet[i].shoot == true)
        {
            putimage(jet[i].x, jet[i].y, &jet[i].img[jet[i].n], SRCINVERT);

            if (jet[i].y > jet[i].hy)
            {
                jet[i].n++; //明暗交替的效果
                jet[i].y -= 5;
            }
            putimage(jet[i].x, jet[i].y, &jet[i].img[jet[i].n], SRCINVERT);
            
            //
            if (jet[i].y <= jet[i].hy)
            {
                putimage(jet[i].x, jet[i].y, &jet[i].img[jet[i].n], SRCINVERT);
                fire[i].x = jet[i].hx + 10;
                fire[i].y = jet[i].hy;
                fire[i].show = true;
                jet[i].shoot = false;
            }
            
            
            jet[i].t1 = jet[i].t2;
        }
    }
}


void Show(DWORD* pMem) //显存指针  像素 这里面所有的像素点的集合
{
    //3D游戏 地形 天空 显存来做
    int drt[16] = { 5, 5, 5, 5, 5, 5, 25, 25, 25, 25, 55, 55, 55, 55, 55, 55 };

    for (int i = 0; i < NUM; i++)
    {
        fire[i].t2 = timeGetTime();
        if (fire[i].t2 - fire[i].t1>fire[i].dt&&fire[i].show == true)
        {
            if (fire[i].r < fire[i].max_r)
            {
                fire[i].r++;
                fire[i].dt = drt[fire[i].r / 10];
                fire[i].draw = true;
            }
            if (fire[i].r >= fire[i].max_r-1)
            {
                fire[i].draw = false;
                Init(i);
            }
            fire[i].t1 = fire[i].t2;
        }
        if (fire[i].draw)
        {
            for (double a = 0; a <= 6.28; a += 0.01)
            {
                int x1 = (int)(fire[i].cen_x + fire[i].r*cos(a));
                int y1 = (int)(fire[i].cen_y + fire[i].r*sin(a));


                if (x1 > 0 && x1<fire[i].width&&y1>0 && y1 < fire[i].height)
                {
                    int b = fire[i].xy[x1][y1] & 0xff;
                    int g = (fire[i].xy[x1][y1] >> 8) & 0xff;
                    int r = (fire[i].xy[x1][y1] >> 16);

                    int xx = (int)(fire[i].x + fire[i].r*cos(a));
                    int yy = (int)(fire[i].y - fire[i].r*sin(a));

                    if (r>0x20 && g > 0x20 && b > 0x20 && xx > 0 && xx<1200 && yy>0 && yy < 800)
                        pMem[yy * 1200 + xx] = BGR(fire[i].xy[x1][y1]);
                }
            }
            fire[i].draw = false;
        }
    }

   

}
搜索更多相关主题的帖子: comment include 结构体 图片 资源 
2017-05-25 23:33



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-477375-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.743477 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved